Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to know if a varcharType is numeric or string?

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2007-10-18 : 10:02:51
Hi All,

How to know if a varchar type is numeric or string?

Let say I have a table called [TABLE1] which has a filed called [ID] of varchar type.
Let say [ID] has following kind of records.

100A23
19876544
2A786B5
98763210
3A756U5

As you can see in above example there are two numeric and three string records.
Is there any function which can tell me which records are numeric and which one is string?

Looking for a quick solution.

Thanks a million in advance.

Zee

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-18 : 10:09:54
this will get all varchars
select * from
WHERE yourColumn LIKE '%[^0-9]%'




_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

bg_elliott
Starting Member

2 Posts

Posted - 2007-10-18 : 10:10:23
I think what you want is the T-SQL function "isNumeric". From the SQL Server documentation:

ISNUMERIC ( expression )


Arguments
expression
Is the expression to be evaluated.

Return Types
int

Remarks
ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 indicates that expression can be converted to at least one of the numeric types.


Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2007-10-18 : 10:11:51
Thanks a lot guys :)
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-18 : 10:29:50
note that isnumeric will return true for
SELECT isnumeric('123d45')
SELECT isnumeric('123e45')

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

jonasalbert20
Constraint Violating Yak Guru

300 Posts

Posted - 2007-10-18 : 22:32:51
SELECT isnumeric('123e45') - "e" is this exponential?
SELECT isnumeric('123d45') - How about "d"?




Want Philippines to become 1st World COuntry? Go for World War 3...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-19 : 02:42:30
Add a dot if you want to allow floating point numbers (which IsNumeric() will give True for of course, so Spirit's is better if you just want Integers)

select * from
WHERE yourColumn LIKE '%[^0-9.]%'

Kristen
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-19 : 04:36:31
here you go:
http://weblogs.sqlteam.com/mladenp/archive/2007/10/18/SQL-Server-Filtering-Numeric-data-from-a-character-based-column.aspx

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-19 : 04:40:55
Not only Isnumeric() has problem but Isdate() as well

Select isdate(2007),isdate('2007'),isdate(2222-22-22),isdate('2222-22-22')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -