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
 extract part of data

Author  Topic 

razeena
Yak Posting Veteran

54 Posts

Posted - 2010-01-31 : 08:06:58
Hi,
My table contains data like this
Description
------------
Feature1-data11-data12
Feature2-data21-data22-data23
Feature3-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 dynamic
I 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 all
select 'Feature2-data21-data22-data23' union all
select '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 @test
where [description] like 'Feature1-%'[/code]


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

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 all
select 'Feature2-data21-data22-data23' union all
select 'Feature3-data31-data32-data33-data34'

Select Right(description,charindex('-',reverse(description))-1)
from @T
where description like 'Feature%'[/code]
Go to Top of Page

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.
Go to Top of Page

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-.
Go to Top of Page
   

- Advertisement -