How to get the numeric part from a string using T-SQL?

后端 未结 4 1315
天命终不由人
天命终不由人 2020-11-30 09:56

I have the following string.

Input
--------------
2030031469-NAI 

To get the numeric part, I am using the following script

         


        
相关标签:
4条回答
  • 2020-11-30 10:26

    In case your string start with alphabet and end with number like ERT-123

    you can use this query:

     (select  substring(@str,patindex('%[0-9]%', @str),len(@str)))
    
    0 讨论(0)
  • 2020-11-30 10:28

    To extract number from an unformatted string

    DECLARE @Text NVARCHAR(100)= 'extract only 23124R integer @#%%'
    SELECT SUBSTRING(@Text, PATINDEX('%[0-9]%',@Text), PATINDEX('%[^0-9]%',SUBSTRING(@Text, PATINDEX('%[0-9]%',@Text), LEN(@Text)))-1) [ONLY_INT]
    
    0 讨论(0)
  • 2020-11-30 10:32
    select left(@str, patindex('%[^0-9]%', @str+'.') - 1)
    
    0 讨论(0)
  • 2020-11-30 10:39

    Please check with this, i used in my project for extracting phone numbers

     CREATE Function [dbo].[RemoveNonNumericCharacters](@Temp VarChar(1000))
        Returns VarChar(1000)
        AS
        Begin
    
            While PatIndex('%[^0-9]%', @Temp) > 0
                Set @Temp = Stuff(@Temp, PatIndex('%[^0-9]%', @Temp), 1, '')
    
            Return @TEmp
        End
    
    0 讨论(0)
提交回复
热议问题