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.
| Author |
Topic |
|
BobRoberts
Posting Yak Master
109 Posts |
Posted - 2012-06-05 : 10:00:23
|
| There is a table TestTable with a UnitOfMeasure column that may contain:case1casecase12case32case6I need to extract the rightmost number, and if there is no digit, have it default to a digit of "1", and put it in a column called ConversionFactor. I need to do this in a SELECT statement (not a stored procedure or function), in the form:SELECT UnitOfMeasure, XXXXXXXXXXXXXXXX As ConversionFactorFROM TestTableSo the query would yield:UnitOfMeasure|ConversionFactorcase1........|...............1case.........|...............1case12.......|..............12case32.......|..............32case6........|...............6 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-06-05 : 10:11:23
|
| reverse, grab all digits, reverse. if no digits, make it 1.How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-05 : 10:19:31
|
if its always case as string you can use likeSELECT CAST(CASE WHEN REPLACE(field,'Case','') > '' THEN REPLACE(field,'Case','') ELSE 1 END AS int) AS Digit FROM table if string is not consistent useSELECT CASE WHEN STUFF(Field,1,PATINDEX('%[0-9]%',Field)-1,'') > '' THEN STUFF(Field,1,PATINDEX('%[0-9]%',Field)-1,'') ELSE 1 END FROM table[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BobRoberts
Posting Yak Master
109 Posts |
Posted - 2012-06-05 : 10:37:49
|
Unfortunately, your answer is too general for me to understand. This is the "Beginning SQL" forum, and if I understood what you mean I probably wouldn't need to ask the question. It's as if I asked you how to bake a cake, and you replied "Mix flour, eggs, sugar in a bowl and bake". Yes, I would understand in general how to bake a cake, but I still wouldn't be able to actually bake one.quote: Originally posted by DonAtWork reverse, grab all digits, reverse. if no digits, make it 1.How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
|
 |
|
|
BobRoberts
Posting Yak Master
109 Posts |
Posted - 2012-06-05 : 10:53:41
|
Yes, it can be anything, so I used your second example, and it worked! Thanks so much, searched everywhere and couldn't find a solution - any solution was using a function or stored procedure to do it in a very bloated fashion. quote: Originally posted by visakh16 if its always case as string you can use likeSELECT CAST(CASE WHEN REPLACE(field,'Case','') > '' THEN REPLACE(field,'Case','') ELSE 1 END AS int) AS Digit FROM table if string is not consistent useSELECT CASE WHEN STUFF(Field,1,PATINDEX('%[0-9]%',Field)-1,'') > '' THEN STUFF(Field,1,PATINDEX('%[0-9]%',Field)-1,'') ELSE 1 END FROM table[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-05 : 12:31:17
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-06-06 : 16:05:20
|
One more waycreate table #tab ( name varchar(20))insert into #tab(name)select 'case1'union allselect 'case'union allselect 'star12'union allselect 'ababab11111'union allselect 'vijay2899022'select name,case when substring(name,(patindex('%[0-9]%',name)),len(name)-(patindex('%[0-9]%',name))+1)=name then 1 else substring(name,(patindex('%[0-9]%',name)),len(name)-(patindex('%[0-9]%',name))+1) end from #tabVijay is here to learn something from you guys. |
 |
|
|
|
|
|
|
|