Query to get only numbers from a string

前端 未结 14 2248
野性不改
野性不改 2020-11-22 06:17

I have data like this:

string 1: 003Preliminary Examination Plan   
string 2: Coordination005  
string 3: Balance1000sheet

The output I exp

14条回答
  •  囚心锁ツ
    2020-11-22 06:32

    Although this is an old thread its the first in google search, I came up with a different answer than what came before. This will allow you to pass your criteria for what to keep within a string, whatever that criteria might be. You can put it in a function to call over and over again if you want.

    declare @String VARCHAR(MAX) = '-123.  a    456-78(90)'
    declare @MatchExpression VARCHAR(255) = '%[0-9]%'
    declare @return varchar(max)
    
    WHILE PatIndex(@MatchExpression, @String) > 0
        begin
        set @return = CONCAT(@return, SUBSTRING(@string,patindex(@matchexpression, @string),1))
        SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
        end
    select (@return)
    

提交回复
热议问题