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
 Find Specific Strings in another String

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 example

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') f

ParseValues can be found below

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544
Go to Top of Page

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 example

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') f

ParseValues can be found below

http://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?
Go to Top of Page

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 example

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') f

ParseValues can be found below

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544



OMG!! it worked perfectly. THANK YOU SO MUCH
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-25 : 13:48:17
welcome
Go to Top of Page

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 example

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') f

ParseValues can be found below

http://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
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-06-26 : 00:32:47
try this one

select 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-') s
inner 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) = ' '

Go to Top of Page

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 example

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (REPLACE(t.yourcol,'LA-',''),'-') f

ParseValues can be found below

http://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 below

SELECT t.yourcol,f.ID,f.Val
FROM yourtable t
CROSS APPLY dbo.ParseValues (SUBSTRING(t.yourcol,CHARINDEX('-',t.yourcol)+1,LEN(t.yourcol)-CHARINDEX('-',t.yourcol)),'-') f
Go to Top of Page
   

- Advertisement -