SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Is this possible?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 12/13/2012 :  10:53:20  Show Profile  Reply with Quote
Hi guys,

I need to make some string to numeric. See following:

Cast(Right(CodeNum, 1) as Int)

But the dilemma is CodeNum has values as ABC3, BBC14. I know I can make the CodeNum as ABC03 and change the Right() function to Right(CodeNum, 2) but for some reason, it will take a lot of work to do that. So what is the easier way to solve this?
Appreciate your time!

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/13/2012 :  11:04:25  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
patindex('%[^0-9]%',CodeNum) will give the position of the first non numeric character
to start from the end patindex('%[^0-9]%',reverse(CodeNum))
So the number of numeric characters is patindex('%[^0-9]%',reverse(CodeNum)) - 1
therefore

convert(int,right(CodeNum,patindex('%[^0-9]%',reverse(CodeNum))-1))



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/13/2012 :  11:19:06  Show Profile  Reply with Quote
wow, this is something looks like c#, thank you so much.
This is great, thanks!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/14/2012 :  01:39:05  Show Profile  Reply with Quote
STUFF(field,1,PATINDEX('%[0-9]%',field)-1,'')

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000