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 2000 Forums
 Transact-SQL (2000)
 Splitting the string

Author  Topic 

Ramnadh
Starting Member

23 Posts

Posted - 2004-05-04 : 03:20:00
Hi,
I have to split the string '$EmpId=1$;$EmpMgr=123$;$DeptNo=20$;'

and get the EmpId,EmpMgr and DeptNo from that string for example i have to get 1,123 and 20. can anyone suggest me, how can we get the ids from the string .

Regards,
Ramnadh.

nr
SQLTeam MVY

12543 Posts

Posted - 2004-05-04 : 03:50:12
You could use replace to get the string in the format n.n.n then use parsename on it.

declare @s varcar(10), @i int, @j int, @val int
select @s = '$EmpMgr='
select @i = charindex($s,@str)
select @i = @i + len(@s)
select @j = charindex('$',@str, @i)
select @val = substring(@str,@i,@j-@i)

Maybe put tyhis in a function and call it with @s.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-05-04 : 04:52:29
Or you could try this:

select substring(mystring, 8, ((charindex('$', mystring, 2) - 1) - 7)) as empid,
substring(mystring, charindex('$EmpMgr=', mystring) + 8, ((charindex('$', mystring, charindex('$EmpMgr=', mystring) + 8)) - (charindex('$EmpMgr=', mystring) + 8))),
right(left(mystring, len(mystring) - 2), charindex('=', (left(reverse(mystring), len(mystring) - 1)))-3) as DeptNo
from mytable


Duane.
Go to Top of Page

Ramnadh
Starting Member

23 Posts

Posted - 2004-05-04 : 05:21:23
Thanx for your reply.

But it is better if the string consists of two parameters. I will get the string parameters between $ and $ upto 6. so i have to take all those parameters from the string. can you help me.

quote:
Originally posted by ditch

Or you could try this:

select substring(mystring, 8, ((charindex('$', mystring, 2) - 1) - 7)) as empid,
substring(mystring, charindex('$EmpMgr=', mystring) + 8, ((charindex('$', mystring, charindex('$EmpMgr=', mystring) + 8)) - (charindex('$EmpMgr=', mystring) + 8))),
right(left(mystring, len(mystring) - 2), charindex('=', (left(reverse(mystring), len(mystring) - 1)))-3) as DeptNo
from mytable


Duane.



Ramnadh
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-05-04 : 05:32:57
I'm not sure what you are asking here.
Can you provide us with sample data and expected results?


Duane.
Go to Top of Page

Ramnadh
Starting Member

23 Posts

Posted - 2004-05-04 : 07:00:43
This is my requirement.
I will get a string with parameters from the Table. I have to take those parameters and get the data from different tables.

The String with the Parameters will be like
'EmpId=99;EmpMgr=123;DeptNo=20;OrgId=2;InchargeId=23'

upto so far the parameters can be limited to maximum of 7 in the string.

I would like to get those in the form of ids means

EmpId=99
EmpMgr=123
DeptNo=20
OrgId=2
InchargeId=23

so that i can keep them in a cursor and get the values and use them accordingly. Previously i thought the string will get in the '$' format which i have send previously but now it was changed.

quote:
Originally posted by Ramnadh

Thanx for your reply.

But it is better if the string consists of two parameters. I will get the string parameters between $ and $ upto 6. so i have to take all those parameters from the string. can you help me.

quote:
Originally posted by ditch

Or you could try this:

select substring(mystring, 8, ((charindex('$', mystring, 2) - 1) - 7)) as empid,
substring(mystring, charindex('$EmpMgr=', mystring) + 8, ((charindex('$', mystring, charindex('$EmpMgr=', mystring) + 8)) - (charindex('$EmpMgr=', mystring) + 8))),
right(left(mystring, len(mystring) - 2), charindex('=', (left(reverse(mystring), len(mystring) - 1)))-3) as DeptNo
from mytable


Duane.



Ramnadh



Ramnadh
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-05-04 : 07:55:35
Beware..."can keep them in a cursor and get the values and use them accordingly"....just be aware of the 'general poor' performance cursors deliver......

search here for examples....
Go to Top of Page

Ramnadh
Starting Member

23 Posts

Posted - 2004-05-04 : 08:02:57
yeh that is my main concern. If we use cursor it is the costliest thing to do. that's why i am asking that is there any other way to get them with the query ?
Thanx for your coordination Andrew.

quote:
Originally posted by AndrewMurphy

Beware..."can keep them in a cursor and get the values and use them accordingly"....just be aware of the 'general poor' performance cursors deliver......

search here for examples....



Ramnadh
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-05-04 : 10:33:51
I would try the function I suggested earlier and see if it is fast enough. It will be the neatest solution and easiest to maintain. Do you have a singlr string to parse or do you have an input dataset?
To alter the function I gave remove the $ from the search string and change the internal literal from $ to ;. You will have to concatenate the string with ; as you don't have a string terminator.

Why are you considering a cursor?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-05-04 : 21:55:20
Another option is the very useful CSV technique:

http://www.sqlteam.com/item.asp?ItemID=2652
--tally table used below
create table numbers ( n int primary key)
declare @n int; set @n=0
while @n <= 255
begin
insert into numbers
select @n
set @n = @n+1
end

declare @input varchar(255)
set @input = 'Empn=99;EmpMgr=123;DeptNo=20;Orgn=2;Inchargen=23'

select
left(input,charindex('=',input,1)-1) [key],
substring(input,charindex('=',input,1)+1,len(input)) [value]
from
(
select NullIf(SubString(';' + @input + ';' , n , CharIndex(';' , ';' + @input + ';' , n) - n) , '') input
from numbers
where n <= Len(';' + @input + ';') AND SubString(';' + @input + ';' , n - 1, 1) = ';'
) d
Go to Top of Page
   

- Advertisement -