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
 General SQL Server Forums
 New to SQL Server Programming
 isnumeric

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-13 : 17:02:02
I have a column (bondinfo) in my warrantdata table that is a mix of alpha and numeric. I would like to select just the numerical values from that column. I know i need to use ISNUMERIC, but not quite sure how. The column is a text column.

Select Bondinfo(ISNUMERIC) from warrantdata

Aint happenin'

thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-13 : 17:06:43
Not sure if it'll work on text data type, but try this:

select * from warrantdata where ISNUMERIC(Bondinfo) = 1

Do you have SQL Server Books Online? A lot of your questions are covered in there in the examples of the particular function. Just want to help you along here and not criticizing you asking questions.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-13 : 17:09:32
I got the following to "work", as i had to cast it:
SELECT ISNUMERIC(Cast (Bondinfo AS VARCHAR(MAX))) FROM WarrantData wd

but the results it's providing are clearly not "true"

Is there any way to use a LIKE, PATINDEX, or CHARINDEX to pull the numbers from that column?

Yes, i refer to the books online (and others) when i'm feeling ambitious.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-13 : 17:14:14
When you want to filter it, you put it in the WHERE clause. Putting it in the select portion just returns the values 1 and 0. You want only the ones with 1, so you put that in the WHERE clause.

select * from warrantdata where ISNUMERIC(cast(Bondinfo as varchar(max))) = 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-13 : 17:19:22
well in that case, it works splendidly. interestingly (and annoyingly) it's also pulling the '$' from that string along with the numeric value. I'll chew on how to lose that. if i can't figure it out, i'll be back.

thanks again!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-13 : 17:28:19
Hint: REPLACE function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-14 : 09:11:41
Yeah, i got that far. My first attempt, hosed that column completely wiping out all data in that column:

UPDATE warrantdata SET bondinfo = REPLACE('$','$','')

I originally thought that script should have looked like this:

UPDATE warrantdata SET bondinfo = REPLACE('bondinfo','$','')

That one wouldn't even fire off, i figured because of the nature of the defecated text column, which is why i used the first version of that ('$','$',''), so i figure i need to recast it in the replace portion, but that one is arguing about the syntax around the REPLACE:

UPDATE warrantdata SET bondinfo = (cast(Bondinfo as varchar(max)))REPLACE(Bondinfo,'$','')

did i also mention that the sql books online make me.........sad?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-14 : 12:32:52
Can you change your data type to be varchar(max) in the table so that you don't have to keep dealing with text conversion issues?

UPDATE warrandata SET bondinfo = REPLACE(cast(Bondinfo as varchar(max)), '$', '')
where ISNUMERIC(cast(Bondinfo as varchar(max))) = 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-14 : 12:43:20
keep this in mind if you're using ISNUMERIC. it wont necessarily be numeric data in all cases

http://beyondrelational.com/modules/2/blogs/70/posts/10803/enhanced-isnumeric-function.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-14 : 12:55:57
The only anomaly i'm seeing are the "$" symbols remaining which i'm trying to get rid of with the 'replace' in the same statement.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-14 : 13:28:48
Did you see what I posted?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-14 : 15:10:18
Sorry, i missed it the first time around. Yes, as i see more of this old data cropping up as TEXT, i'm going to start casting and converting that data first off on each project. Thanks for your help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-14 : 15:16:47
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -