HÀM TÁCH SỐ VÀ CHỮ KHỎI CHUỖI

-- HÀM TÁCH SỐ RA KHỎI CHUỖI
Create function UDF_ExtractNumbers
(  
  @input varchar(255)  
)  
Returns varchar(255)  
As  
Begin  
  Declare @alphabetIndex int = Patindex('%[^0-9]%', @input)  
  Begin  
    While @alphabetIndex > 0  
    Begin  
      Set @input = Stuff(@input, @alphabetIndex, 1, '' )  
      Set @alphabetIndex = Patindex('%[^0-9]%', @input )  
    End  
  End  
  Return @input
End
---- HÀM TÁCH CHỮ RA KHỎI CHUỖI
Create function UDF_ExtractAlphabets
(  
  @input varchar(255)  
)  
Returns varchar(255)  
As  
Begin  
  Declare @alphabetIndex int = Patindex('%[^a-zA-Z]%', @input)  
  Begin  
    While @alphabetIndex > 0  
    Begin  
      Set @input = Stuff(@input, @alphabetIndex, 1, '' )  
      Set @alphabetIndex = Patindex('%[^a-zA-Z]%', @input )  
    End  
  End  
  Return @input
End

Last updated