Query to get only numbers from a string

前端 未结 14 2271
野性不改
野性不改 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:19

    With the previous queries I get these results:

    'AAAA1234BBBB3333' >>>> Output: 1234

    '-çã+0!\aº1234' >>>> Output: 0

    The code below returns All numeric chars:

    1st output: 12343333

    2nd output: 01234

    declare @StringAlphaNum varchar(255)
    declare @Character varchar
    declare @SizeStringAlfaNumerica int
    declare @CountCharacter int
    
    set @StringAlphaNum = 'AAAA1234BBBB3333'
    set @SizeStringAlfaNumerica = len(@StringAlphaNum)
    set @CountCharacter = 1
    
    while isnumeric(@StringAlphaNum) = 0
    begin
        while @CountCharacter < @SizeStringAlfaNumerica
            begin
                if substring(@StringAlphaNum,@CountCharacter,1) not like '[0-9]%'
                begin
                    set @Character = substring(@StringAlphaNum,@CountCharacter,1)
                    set @StringAlphaNum = replace(@StringAlphaNum, @Character, '')
                end
        set @CountCharacter = @CountCharacter + 1
        end
        set @CountCharacter = 0
    end
    select @StringAlphaNum
    

提交回复
热议问题