Query to get only numbers from a string

前端 未结 14 2252
野性不改
野性不改 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:25
    declare @puvodni nvarchar(20)
    set @puvodni = N'abc1d8e8ttr987avc'
    
    WHILE PATINDEX('%[^0-9]%', @puvodni) > 0 SET @puvodni = REPLACE(@puvodni, SUBSTRING(@puvodni, PATINDEX('%[^0-9]%', @puvodni), 1), '' ) 
    
    SELECT @puvodni
    
    0 讨论(0)
  • 2020-11-22 06:27

    Try this one -

    Query:

    DECLARE @temp TABLE
    (
          string NVARCHAR(50)
    )
    
    INSERT INTO @temp (string)
    VALUES 
        ('003Preliminary Examination Plan'),
        ('Coordination005'),
        ('Balance1000sheet')
    
    SELECT LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1) 
    FROM (
        SELECT subsrt = SUBSTRING(string, pos, LEN(string))
        FROM (
            SELECT string, pos = PATINDEX('%[0-9]%', string)
            FROM @temp
        ) d
    ) t
    

    Output:

    ----------
    003
    005
    1000
    
    0 讨论(0)
  • 2020-11-22 06:29

    T-SQL function to read all the integers from text and return the one at the indicated index, starting from left or right, also using a starting search term (optional):

    create or alter function dbo.udf_number_from_text(
        @text nvarchar(max),
        @search_term nvarchar(1000) = N'',
        @number_position tinyint = 1,
        @rtl bit = 0
    ) returns int
    as
        begin
            declare @result int = 0;
            declare @search_term_index int = 0;
    
            if @text is null or len(@text) = 0 goto exit_label;
            set @text = trim(@text);
            if len(@text) = len(@search_term) goto exit_label;
    
            if len(@search_term) > 0
                begin
                    set @search_term_index = charindex(@search_term, @text);
                    if @search_term_index = 0 goto exit_label;
                end;
    
            if @search_term_index > 0
                if @rtl = 0
                    set @text = trim(right(@text, len(@text) - @search_term_index - len(@search_term) + 1));
                else
                    set @text = trim(left(@text, @search_term_index - 1));
            if len(@text) = 0 goto exit_label;
    
            declare @patt_number nvarchar(10) = '%[0-9]%';
            declare @patt_not_number nvarchar(10) = '%[^0-9]%';
            declare @number_start int = 1;
            declare @number_end int;
            declare @found_numbers table (id int identity(1,1), val int);
    
            while @number_start > 0
            begin
                set @number_start = patindex(@patt_number, @text);
                if @number_start > 0
                    begin
                        if @number_start = len(@text)
                            begin
                                insert into @found_numbers(val)
                                select cast(substring(@text, @number_start, 1) as int);
    
                                break;
                            end;
                        else
                            begin
                                set @text = right(@text, len(@text) - @number_start + 1);
                                set @number_end = patindex(@patt_not_number, @text);
    
                                if @number_end = 0
                                    begin
                                        insert into @found_numbers(val)
                                        select cast(@text as int);
    
                                        break;
                                    end;
                                else
                                    begin
                                        insert into @found_numbers(val)
                                        select cast(left(@text, @number_end - 1) as int);
    
                                        if @number_end = len(@text)
                                            break;
                                        else
                                            begin
                                                set @text = trim(right(@text, len(@text) - @number_end));
                                                if len(@text) = 0 break;
                                            end;
                                    end;
                            end;
                    end;
            end;
    
            if @rtl = 0
                select @result = coalesce(a.val, 0)
                from (select row_number() over (order by m.id asc) as c_row, m.val
                        from @found_numbers as m) as a
                where a.c_row = @number_position;
            else
                select @result = coalesce(a.val, 0)
                from (select row_number() over (order by m.id desc) as c_row, m.val
                        from @found_numbers as m) as a
                where a.c_row = @number_position;
    
    
            exit_label:
                return @result;
        end;
    

    Example:

    select dbo.udf_number_from text(N'Text text 10 text, 25 term', N'term',2,1);
    

    returns 10;

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 06:33

    Please try:

    declare @var nvarchar(max)='Balance1000sheet'
    
    SELECT LEFT(Val,PATINDEX('%[^0-9]%', Val+'a')-1) from(
        SELECT SUBSTRING(@var, PATINDEX('%[0-9]%', @var), LEN(@var)) Val
    )x
    
    0 讨论(0)
  • 2020-11-22 06:35

    This UDF will work for all types of strings:

    CREATE FUNCTION udf_getNumbersFromString (@string varchar(max))
    RETURNS varchar(max)
    AS
    BEGIN
        WHILE  @String like '%[^0-9]%'
        SET    @String = REPLACE(@String, SUBSTRING(@String, PATINDEX('%[^0-9]%', @String), 1), '')
        RETURN @String
    END
    
    0 讨论(0)
提交回复
热议问题