Query to get only numbers from a string

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

    If you are using Postgres and you have data like '2000 - some sample text' then try substring and position combination, otherwise if in your scenario there is no delimiter, you need to write regex:

    SUBSTRING(Column_name from 0 for POSITION('-' in column_name) - 1) as 
    number_column_name
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 06:21

    I did not have rights to create functions but had text like

    ["blahblah012345679"]
    

    And needed to extract the numbers out of the middle

    Note this assumes the numbers are grouped together and not at the start and end of the string.

    select substring(column_name,patindex('%[0-9]%', column_name),patindex('%[0-9][^0-9]%', column_name)-patindex('%[0-9]%', column_name)+1)
    from table name
    
    0 讨论(0)
  • 2020-11-22 06:22

    First create this UDF

    CREATE FUNCTION dbo.udf_GetNumeric
    (
      @strAlphaNumeric VARCHAR(256)
    )
    RETURNS VARCHAR(256)
    AS
    BEGIN
      DECLARE @intAlpha INT
      SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
      BEGIN
        WHILE @intAlpha > 0
        BEGIN
          SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
          SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
        END
      END
      RETURN ISNULL(@strAlphaNumeric,0)
    END
    GO
    

    Now use the function as

    SELECT dbo.udf_GetNumeric(column_name) 
    from table_name
    

    SQL FIDDLE

    I hope this solved your problem.

    Reference

    0 讨论(0)
  • 2020-11-22 06:23

    Just a little modification to @Epsicron 's answer

    SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%', 
                        string) + 1) AS Number
    FROM (values ('003Preliminary Examination Plan'),
        ('Coordination005'),
        ('Balance1000sheet')) as a(string)
    

    no need for a temporary variable

    0 讨论(0)
  • 2020-11-22 06:24

    Query:

    DECLARE @temp TABLE
    (
        string NVARCHAR(50)
    )
    
    INSERT INTO @temp (string)
    VALUES 
        ('003Preliminary Examination Plan'),
        ('Coordination005'),
        ('Balance1000sheet')
    
    SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%', 
                        string) + 1) AS Number
    FROM @temp
    
    0 讨论(0)
提交回复
热议问题