Is there a function which determines if a varchar is a number?

  • Hi is there a function which determines if a varchar is a number?

    E.g.

    Select * from table where filed <> to a number

  • you can try the isnumeric function:

    WHERE ISNUMERIC(SOMEVARCHAR) = 1

    but as others will tell you, it's not 100% reliable, because of the way it is structured; for exampel

    select isnumeric('1E2')

    select isnumeric('1d2')

    all return 1(ttue, because E means exponent, or d means decimal...so it is possible a bad value could get thru;

    I've got these two functions saved in my snippets, isReallyNumeric and isReallyInteger, which are a refinement of the question in a previous thread.

    CREATE FUNCTION dbo.isReallyNumeric

    (

    @num VARCHAR(64)

    )

    RETURNS BIT

    BEGIN

    IF LEFT(@num, 1) = '-'

    SET @num = SUBSTRING(@num, 2, LEN(@num))

    DECLARE @pos TINYINT

    SET @pos = 1 + LEN(@num) - CHARINDEX('.', REVERSE(@num))

    RETURN CASE

    WHEN PATINDEX('%[0-9.-]%', @num) = 0

    AND @num NOT IN ('.', '-', '+', '')

    AND LEN(@num)>0

    AND @num NOT LIKE '%-%'

    AND

    (

    ((@pos = LEN(@num)+1)

    OR @pos = CHARINDEX('.', @num))

    )

    THEN

    1

    ELSE

    0

    END

    END

    GO

    CREATE FUNCTION dbo.isReallyInteger

    (

    @num VARCHAR(64)

    )

    RETURNS BIT

    BEGIN

    IF LEFT(@num, 1) = '-'

    SET @num = SUBSTRING(@num, 2, LEN(@num))

    RETURN CASE

    WHEN PATINDEX('%[0-9-]%', @num) = 0

    AND CHARINDEX('-', @num) 1

    AND @num NOT IN ('.', '-', '+', '')

    AND LEN(@num)>0

    AND @num NOT LIKE '%-%'

    THEN

    1

    ELSE

    0

    END

    END

    GO

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Many thanks extremely helpful

  • Edward (11/3/2008)


    Hi is there a function which determines if a varchar is a number?

    What kind of number?

    Integer, decimal, float?

    Is "$100.00-" a number for your application?

    For some it is.

    _____________
    Code for TallyGenerator

  • Lowell (11/3/2008)


    you can try the isnumeric function:

    WHERE ISNUMERIC(SOMEVARCHAR) = 1

    but as others will tell you, it's not 100% reliable, because of the way it is structured; for exampel

    select isnumeric('1E2')

    select isnumeric('1d2')

    all return 1(ttue, because E means exponent, or d means decimal...so it is possible a bad value could get thru;

    I've got these two functions saved in my snippets, isReallyNumeric and isReallyInteger, which are a refinement of the question in a previous thread.

    [font="Courier New"]CREATE FUNCTION dbo.isReallyNumeric  

    (  

    @num VARCHAR(64)  

    )  

    RETURNS BIT  

    BEGIN  

    IF LEFT(@num, 1) = '-'  

    SET @num = SUBSTRING(@num, 2, LEN(@num))  

    DECLARE @pos TINYINT  

    SET @pos = 1 + LEN(@num) - CHARINDEX('.', REVERSE(@num))  

    RETURN CASE  

    WHEN PATINDEX('%[0-9.-]%', @num) = 0  

    AND @num NOT IN ('.', '-', '+', '')

    AND LEN(@num)>0  

    AND @num NOT LIKE '%-%'

    AND  

    (  

    ((@pos = LEN(@num)+1)  

    OR @pos = CHARINDEX('.', @num))  

    )  

    THEN  

    1  

    ELSE  

    0  

    END  

    END  

    GO  

    CREATE FUNCTION dbo.isReallyInteger  

    (  

    @num VARCHAR(64)  

    )  

    RETURNS BIT  

    BEGIN  

    IF LEFT(@num, 1) = '-'  

    SET @num = SUBSTRING(@num, 2, LEN(@num))  

    RETURN CASE  

    WHEN PATINDEX('%[0-9-]%', @num) = 0  

    AND CHARINDEX('-', @num) <= 1  

    AND @num NOT IN ('.', '-', '+', '')

    AND LEN(@num)>0  

    AND @num NOT LIKE '%-%'

    THEN  

    1  

    ELSE  

    0  

    END  

    END  

    GO[/font]

    Lowell, what's the previous post link? Just curious, but wouldn't it be just as easy to multiply by one and see if you get a result or an error?

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • the previous link was a 20+ page forum discussion here on SSC;When I used the Google tool, it did not find the thread; I grabbed what items I thought were useful from it, and these were two of them.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • thanks, I couldn't find it either.

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • Thanks for this Script. It really helped me alot. Since i`m developing for an german enterprise i rewrote the script to use "," instead of ".". I also asume that the last char has to be a number:

    USE [IFCC]

    GO

    /****** Objekt: UserDefinedFunction [dbo].[isReallyNumeric] Skriptdatum: 04/16/2009 13:51:10 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    ALTER FUNCTION [dbo].[isReallyNumeric]

    (

    @num VARCHAR(64)

    )

    RETURNS BIT

    BEGIN

    IF LEFT(@num, 1) = '-'

    SET @num = SUBSTRING(@num, 2, LEN(@num))

    DECLARE @pos TINYINT

    SET @pos = 1 + LEN(@num) - CHARINDEX(',', REVERSE(@num))

    RETURN CASE

    WHEN PATINDEX('%[^0-9,-]%', @num) = 0

    AND PATINDEX('[0-9]%',REVERSE(@num)) != 0 --the last digit has to be a number

    AND @num NOT IN (',', '-', '+', '^')

    AND LEN(@num)>0

    AND @num NOT LIKE '%-%'

    AND

    (

    ((@pos = LEN(@num)+1) --there is no ,

    OR @pos = CHARINDEX(',', @num)) --there is only 1 ,

    )

    THEN

    1

    ELSE

    0

    END

    END

  • glad you found it Dom;

    this site has a Ton of really useful scripts. enjoy.

    i cleaned up my original post, since the update to the CODE window made HTML font info look bad .

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • How about this code?

    Declare @STR varchar(30)

    select @STR = 'karTHI&%@#123'

    select substring(@Str,N,1),ascii(substring(@Str,N,1)) as AsciiValue,

    case when (ascii(substring(@Str,N,1)) >= 65 and ascii(substring(@Str,N,1)) = 97 and ascii(substring(@Str,N,1)) <= 122)

    or

    (ascii(substring(@Str,N,1)) in (35,36,37,38,64))

    then 1 else 0 end as Status

    from Tally

    where N <= len(@Str)

    karthik

  • why 9 returns false for IsRealyNumeric?

  • Edward-445599 (11/3/2008)


    Hi is there a function which determines if a varchar is a number?

    E.g.

    Select * from table where filed <> to a number

    Let's ask a question here... is the intent to determine if the "field" contains only digits from 0 to 9???

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply