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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Extracting delimited string

Author  Topic 

LaurieCox

158 Posts

Posted - 2008-09-05 : 15:12:28
Hi,

Problem: Extract data from varchar field that is between the $ delimiters

My Solution (which works):


CREATE TABLE #DataToParse (StringData varchar(100),
TestCase varchar(100))

INSERT INTO #DataToParse (StringData,TestCase)
select 'lkasdjf s$xxxxxxxxx$lasdjf;kjaklj a;sdkjf','hidden in the middle' union all
select '$asdkfjaskj xxx$' ,'no other data' union all
select 'aklsdfj ;kjasdf asklfj' ,'not there' union all
select ' $aksdjf as;dfkljy$' ,'at the end' union all
select 'lskdjf s$$laksdjf asdf' ,'no data between' union all
select 'asdfjsdf$kalsdjf askfj' ,'only one in the middle' union all
select '$ksdjf sk;dfj askdfj ' ,'only one at the beginning' union all
select 'ksdjf sk;dfj askdfj $' ,'only one at the end' union all
select '$$', 'just $$' union all
select '$', 'just $' union all
select 'klasdjfa$kasdjf$asldkjf asd$','three $s'


select StringData,
TestCase,
Substring
(StringData,
charindex('$',StringData)+1,
charindex('$',StringData,
charindex('$',StringData,
charindex('$',StringData)+1)) -
charindex('$',StringData)-1) as Result
from #DataToParse
where StringData like '%$%$%'


I figured this out by reading about the string functions in the BOL. The thing is there might be a "Better* Way" to do this. I don't want somebody looking at my code and saying: Why did you do it that way, when you could have should have just done it "Better Way".

So my question is: Is there a better way to do this?

* I know better is a relative term.

Thanks,

Laurie

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-14 : 09:18:12
select *
from #datatoparse
cross apply dbo.fnParseList('$', stringdata)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

LaurieCox

158 Posts

Posted - 2008-09-19 : 16:29:23
Hi Peso,

Just noticed that I got a reply. Anyway i tried your answer and got this error:

Line 20: Incorrect syntax near 'apply'.

Then I noticed that I was running it in a query window that was pointing to a non upgraded database.

So I switched to database running in 2005 and got this error:

Invalid object name 'dbo.fnParseList'.

I tried to find fnParseList in BOL and got no results found.

Then I searched here and found this thread. I assume you are referring the to function fnParseList found there.

I don't have time to fool with it now, but you have given me some new stuff to study. I found information about "using apply" in the BOL looks interesting.

Anyway, thanks for your help.

Laurie
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-19 : 16:35:59
Can you have data like this:

"$abc$ $def$"

?

If so, what should be returned?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-19 : 16:37:25
Peso -- please try to remember to add links when referencing custom functions like that one in solutions.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-20 : 01:49:21
quote:
Originally posted by AlgaeSea

Hi Peso,

Just noticed that I got a reply. Anyway i tried your answer and got this error:

Line 20: Incorrect syntax near 'apply'.

Then I noticed that I was running it in a query window that was pointing to a non upgraded database.

So I switched to database running in 2005 and got this error:

Invalid object name 'dbo.fnParseList'.

I tried to find fnParseList in BOL and got no results found.

Then I searched here and found this thread. I assume you are referring the to function fnParseList found there.

I don't have time to fool with it now, but you have given me some new stuff to study. I found information about "using apply" in the BOL looks interesting.

Anyway, thanks for your help.

Laurie



APPLY operator works only in SQL 2005 databases with compatability level set to 90
Go to Top of Page
   

- Advertisement -