is varchar numeric

5 pts.
Tags:
SQL
VARCHAR
sql to check varchar as numeric

Answer Wiki

Thanks. We'll let you know when a new response is added.

Use the IS_NUMERIC() function.

[kccrosser] A more generic solution is to simply try to assign the value to an integer inside a Try..Catch block. E.g.:

– SQL Server version
declare @i integer
declare @vc varchar(255)

begin try
set @i = @vc
end try
begin catch
– if we get here, the value in @vc isn’t a valid integer
end catch

– Oracle PL/SQL version
declare i int
declare vc varchar2(255)

begin
i := vc;
exception
when VALUE_ERROR then — sqlcode = -6502
– handle bad numeric error
end;

If you want to handle floating point/decimal values, just make sure the receiving variable is of the correct data type.

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Meandyou
    I did not recall the "IS_NUMERIC" function. To my knowledge this must be outside the ANSI standards. By definition, in an RDBMS, if one wants numbers then one should use a numeric data type such as INTEGER, DECIMAL, FLOAT or whatever is appropriate for your data and within the confines of your particular RDBMS. A VARCHAR column, again by definition, can contain any characters. One old approach to your problem that I recall involved a CASE statement and substringing thru the column one character at a time ... ,CASE WHEN ONE_CHAR BETWEEN '0' AND '9' THEN 'NUMBER' But If it is important to know if a CHARACTER column contains NUMERICS, then I would contend that something is wrong with the design of your system or application
    5,220 pointsBadges:
    report
  • carlosdl
    You are right Meandyou, it is not a standard function. I think it is a t-sql function, but actually the function is ISNUMERIC() not IS_NUMERIC(). Depending on the RDBMS, there could be different ways to do it. In Oracle, for example, one option could be performing a to_number() conversion, and catching the specific error (ORA-06502) that should be thrown if the argument wasn't numeric.
    69,835 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following