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 2005 Forums
 Transact-SQL (2005)
 splitting a ref code

Author  Topic 

JezLisle
Posting Yak Master

132 Posts

Posted - 2010-01-25 : 11:28:26
I have a VARCHAR reference field that is like LB1 to LB500

How can I split the numeric value out and use it in a field that is and INT datatype?

having a blank day

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-25 : 11:43:44
See madhis solution:
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/18/extract-only-numbers-from-a-string.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-01-25 : 13:03:06
Are the first 2 bytes alway Alpha?

We need more sample Data

But could be as simple as


SELECT CONVERT(int, SUBSTRING(Col1,3,LEN(COL1) -1))
FROM (
SELECT 'LB1' AS Col1 UNION ALL
SELECT 'LB500' AS Col1
) AS XXX




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-27 : 02:57:50
quote:
Originally posted by X002548

Are the first 2 bytes alway Alpha?

We need more sample Data

But could be as simple as


SELECT CONVERT(int, SUBSTRING(Col1,3,LEN(COL1) -1))
FROM (
SELECT 'LB1' AS Col1 UNION ALL
SELECT 'LB500' AS Col1
) AS XXX




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam






Would you expect 0 for LB?

Madhivanan

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

- Advertisement -