Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Is there a way to return the right part of a string untill a certain character is encountered.In the sample data below i know a freetext field is at the end of my string and i want to separate this ou as exampled below.Any advice would be great!Thanks---------------------------------------------/*Please paste T-SQL into query window*/---------------------------------------------CREATE TABLE #sample(Field1 nvarchar(100))Insert into #sampleselect '#"XPELV"#"1802"#"1006"#"04/01/11"freetext' UNION allselect '#"07/01/11"#"XCHES"#"1400"#"1400"#"07/01/11"Chest X-ray' select * from #sample--This is an example of the desired outputCREATE TABLE #desiredoutput(Field1 nvarchar(100))insert into #desiredoutputselect 'freetext' UNION allselect 'Chest X-ray' select * from #desiredoutputdrop table #sampledrop table #desiredoutput
DLTaylor
Posting Yak Master
136 Posts
Posted - 2011-01-07 : 12:21:17
I suppose i should add the character i encounter in my example is the " characterThanks
jimf
Master Smack Fu Yak Hacker
2875 Posts
Posted - 2011-01-07 : 12:26:32
Maybe this?Jim
select field1 REVERSE(STUFF ( reverse(Field1) -- into here ,charindex( '"',reverse(Field1)) --starting here ,100000 , '' --this value ) ) from #sample
Everyday I learn something that somebody else already knew
DLTaylor
Posting Yak Master
136 Posts
Posted - 2011-01-07 : 12:34:10
Thanks - im getting incorrect syntax near STUFF?
DLTaylor
Posting Yak Master
136 Posts
Posted - 2011-01-07 : 12:41:20
Sorry my mistake ! code works perfectly!Thanks for your help jimf ;-)
jimf
Master Smack Fu Yak Hacker
2875 Posts
Posted - 2011-01-07 : 12:43:41
You're welcome!JimEveryday I learn something that somebody else already knew