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 |
|
mathmath
Starting Member
13 Posts |
Posted - 2009-06-24 : 12:40:10
|
| Hi everyone, I am working on a project where i have encounter such problems:Data samples:LA-1-2,LA-1-2-33,LA-1-22-333-4444,LA-2-3,LA-12-34,LA-123-45-67..with upto 8 dashes.Objective:I need to find the values behind each dashes. [e.g LA-1-2-33 will have columnA = 1,columnB = 2,columnC=33...etc.].Since there are many more situations,i dont know how to query it. [there is a way by using declare & count but very tedious].Thanks Very Much! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-24 : 12:44:17
|
| just use a function to parse the string and seperate the values b/w - characters. you can try below as an exampleSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') fParseValues can be found belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544 |
 |
|
|
mathmath
Starting Member
13 Posts |
Posted - 2009-06-24 : 20:23:39
|
quote: Originally posted by visakh16 just use a function to parse the string and seperate the values b/w - characters. you can try below as an exampleSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') fParseValues can be found belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544
*Does this work on MsSQL?*I read the ariticle. This maybe obviouse, but how do you use a function that you just created? |
 |
|
|
mathmath
Starting Member
13 Posts |
Posted - 2009-06-25 : 01:54:48
|
quote: Originally posted by visakh16 just use a function to parse the string and seperate the values b/w - characters. you can try below as an exampleSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') fParseValues can be found belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544
OMG!! it worked perfectly. THANK YOU SO MUCH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-25 : 13:48:17
|
welcome |
 |
|
|
mathmath
Starting Member
13 Posts |
Posted - 2009-06-25 : 23:54:53
|
quote: Originally posted by visakh16 just use a function to parse the string and seperate the values b/w - characters. you can try below as an exampleSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') fParseValues can be found belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544
*I encounter another problem:What happen if my Data has various 'L's, such as LB-, L123-, how should i deal with it?THanks |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-06-26 : 00:32:47
|
try this oneselect s.yourcol,substring(yourcol,charindex('-',yourcol)+1,abs(charindex('-',yourcol,charindex('-',yourcol,charindex('-',yourcol)+1))-charindex('-',yourcol,1)-1)) as id,case when charindex('-',yourcol,charindex('-',yourcol)+1)>0 then right(yourcol,charindex('-',yourcol)-1) else ''end as [value]from (select 'la-4-55'as yourcol union all select 'ht-5-67' union all select 'la-') sinner join master..spt_values v on type ='p' and v.number > 0 and v.number <= len(s.yourcol)where substring(' ' + s.yourcol, v.number, 1) = ' ' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-26 : 13:46:04
|
quote: Originally posted by mathmath
quote: Originally posted by visakh16 just use a function to parse the string and seperate the values b/w - characters. you can try below as an exampleSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') fParseValues can be found belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544
*I encounter another problem:What happen if my Data has various 'L's, such as LB-, L123-, how should i deal with it?THanks
then make it like belowSELECT t.yourcol,f.ID,f.ValFROM yourtable tCROSS APPLY dbo.ParseValues (SUBSTRING(t.yourcol,CHARINDEX('-',t.yourcol)+1,LEN(t.yourcol)-CHARINDEX('-',t.yourcol)),'-') f |
 |
|
|
|
|
|
|
|