Get only the number from a string, T-SQL???

Last post 08-29-2005 10:36 AM by pkr. 6 replies.

Sort Posts:

  • Get only the number from a string, T-SQL???

    08-26-2005, 3:41 PM
    I have a string in form "abcdefg 12355 ijklmn"

    Now I want get only the number 12355 within the string !!
    Is there any function available in T-SQL of Sql server 2K??

    Thanks
    for any help
  • Re: Get only the number from a string, T-SQL???

    08-27-2005, 2:28 AM
    • All-Star
      46,980 point All-Star
    • ndinakar
    • Member since 05-05-2003, 4:57 PM
    • Redmond, WA
    • Posts 6,858
    • Moderator
      TrustedFriends-MVPs
    I dont think there is any off the shelf function. You would need to write your own custom function. SQL Server is not really good at string manipulations. You could do this very easily using regular expressions at the front end.
    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
  • Re: Get only the number from a string, T-SQL???

    08-27-2005, 3:03 AM

    CREATE FUNCTION dbo.fFilterNumeric
    (
    @Src nvarchar(255)
    )
    RETURNS nvarchar(255)
    AS
    BEGIN
    declare @Res nvarchar(255)
    declare @i int, @l int, @c char
    select @i=1, @l=len(@Src)
    SET @Res = ''
    while @i<=@l
    begin
       set @c=upper(substring(@Src,@i,1))
        IF  isnumeric(@c)=1
            SET @Res = @Res + @c
       set @i=@i+1
    end

    return(@res)
    END

     

  • Re: Get only the number from a string, T-SQL???

    08-27-2005, 11:54 AM
    • All-Star
      42,319 point All-Star
    • tmorton
    • Member since 08-06-2002, 9:37 PM
    • SE Pennsylvania, USA
    • Posts 7,031
    • ASPInsiders
      Moderator
    The issue I have with IndianScorpion's suggestion is the use of the ISNUMERIC function.  This function will also return a 1 for some characters you'd might not expect, such as a comma (,), dollar sign ($), and a period (.).

    Try this little script in Query Analyzer to see the ASCII characters between 1 and 254 that SQL Server considers to be numeric:
    DECLARE @myTest char(10), @a int
    SELECT @a = 1
    WHILE @a < 255
        BEGIN
            SELECT @myTest = CHAR(@a)
            IF ISNUMERIC(@myTest) = 1
                PRINT CAST(@a AS char(3)) + ' -- ' + @myTest
            SELECT @a = @a + 1
        END

    I would do something like this instead:
    IF  @c IN ('0','1','2','3','4','5','6','7','8','9')

    Terri Morton
    ASP.NET Website Manager, Neudesic
  • Re: Get only the number from a string, T-SQL???

    08-29-2005, 8:01 AM
    • Member
      10 point Member
    • pmz0178
    • Member since 08-29-2005, 10:57 AM
    • Posts 2
    dont like long cycles,
    if  needs  cut just  one number without dot (by Ukrainian - крапки),
    and number always exists,
    try example below,  this just example,  may this example transform like one select

    ---
    Declare @X     varchar(100)
    Select     @X=    'Here is where15234Numbers'
    --
    Select     @X=    SubString(@X,PATINDEX('%[0-9]%',@X),Len(@X))
    Select     @X=    SubString(@X,0,PATINDEX('%[^0-9]%',@X))
    --// show result
    Select     @X



  • Re: Get only the number from a string, T-SQL???

    08-29-2005, 9:08 AM
    • All-Star
      42,319 point All-Star
    • tmorton
    • Member since 08-06-2002, 9:37 PM
    • SE Pennsylvania, USA
    • Posts 7,031
    • ASPInsiders
      Moderator
     pmz0178 wrote:

    Declare @X     varchar(100)
    Select     @X=    'Here is where15234Numbers'
    --
    Select     @X=    SubString(@X,PATINDEX('%[0-9]%',@X),Len(@X))
    Select     @X=    SubString(@X,0,PATINDEX('%[^0-9]%',@X))
    --// show result
    Select     @X


    The limitation of this solution is that a value of @X like this:
    Select     @X=    'Here is where15234Numbers987'
    will result in 15234, and not 15234987, as IndianScorpion's would.  This could be a positive thing or a negative thing, depending on the situation.

    Terri Morton
    ASP.NET Website Manager, Neudesic
  • Re: Get only the number from a string, T-SQL???

    08-29-2005, 10:36 AM
    • Star
      9,141 point Star
    • pkr
    • Member since 02-24-2004, 6:01 AM
    • Posts 1,835
    I suggest that TSQL is the wrong tool for the job, at least until you can use the CLR. IMO much better if you can use a client to do this work before it gets to the database, a simple (well when are they) regular expression would sort this kind of problem out.
Page 1 of 1 (7 items)