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 |
|
razeena
Yak Posting Veteran
54 Posts |
Posted - 2010-01-31 : 08:06:58
|
| Hi, My table contains data like thisDescription------------Feature1-data11-data12Feature2-data21-data22-data23Feature3-data31-data32-data33-data34---------------------- Feature1,Feature2 and Feature3 are static words entered into db and they won't change. But the data appearing after it which will be separated by a dash is dynamicI want to retrieve the last data appearing after last dash corresponding to feature1(in above eg, it is data12). |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-31 : 08:24:42
|
[code]-- making test data...declare @test table ([description] varchar(255))insert @test select 'Feature1-data11-data12' union allselect 'Feature2-data21-data22-data23' union allselect 'Feature3-data31-data32-data33-data34'-- display test data...select * from @test-- extract the wanted part...select reverse(substring(reverse([description]),1,charindex('-',reverse([description]))-1))from @testwhere [description] like 'Feature1-%'[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2010-01-31 : 09:22:48
|
| [code]declare @T table (description varchar(255))insert @test select 'Feature1-data11-data12' union allselect 'Feature2-data21-data22-data23' union allselect 'Feature3-data31-data32-data33-data34'Select Right(description,charindex('-',reverse(description))-1)from @Twhere description like 'Feature%'[/code] |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-31 : 09:49:04
|
Hello sodeep,ok - that looks less complicated But like 'Feature1-%' would be better since OP wanted output for Feature1. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2010-01-31 : 09:51:58
|
Thanks Webfred.Well I thought that was just example so I removed 1-. |
 |
|
|
|
|
|