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)
 comma seperated select

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2008-05-14 : 15:50:18
Not sure the best way to do this. They are passing in a comma seperated string. I have a field that has comma seperated strings in it. I need to compare the 2 strings and do a select on that field that has any of the words in the string passed in. So if the pass...
"car,boat,van"

and in the field 2 of the 3 records get sent back
car,train,bike
boat,truck,plane
bike,plane,train

I would send them back the top 2 because the top one contains car and the second contains boat

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-05-14 : 16:44:26
Not the prettiest solution but it works.
declare @string varchar(30)
set @string = 'car, boat, van'

declare @lists table ( list varchar(30) )
insert @lists ( list )
select 'car, train, bike' union
select 'boat, truck, plane' union
select 'bike, plane, train'
;
with z ( rn, list )
as ( select a.rn, a.list
from ( select row_number() over ( order by list ) as rn,
list
from @lists
) a
union all
select a.rn,
left(z.list, len(z.list) - charindex(',', reverse(z.list)))
from ( select row_number() over ( order by list ) as rn,
list
from @lists
) a
join z on a.rn = z.rn
where charindex(',', z.list) > 0
)
select z2.list
from z z1
join ( select rn, max(list) as list
from z
group by rn
) z2 on z1.rn = z2.rn
where charindex(ltrim(right(z1.list, isnull(nullif(charindex(',', reverse(z1.list)) - 1, -1), len(z1.list)))), @string) > 0

Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2008-05-15 : 07:04:55
I am getting these errors
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'with'.
Msg 195, Level 15, State 10, Line 12
'row_number' is not a recognized function name.
Msg 195, Level 15, State 10, Line 19
'row_number' is not a recognized function name.
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-05-15 : 07:14:08
quote:
Originally posted by helixpoint

I am getting these errors
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'with'.
Msg 195, Level 15, State 10, Line 12
'row_number' is not a recognized function name.
Msg 195, Level 15, State 10, Line 19
'row_number' is not a recognized function name.



ROW_NUMBER() IS IN SQL SERVER 2005 BUT IT IS NOT IN SQL SERVER 2000. WHICH VERSION U ARE USING??
Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2008-05-15 : 08:43:59
Anyway to do a distinct on this?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-05-15 : 08:54:00
Any chance you can fix your database design and normalize it? You should never store comma-separated data in your tables; it should be broken out into multiple rows in a related table. With a proper database design, this is pretty easy and efficient; without a good database design, it will be slow and pretty complicated and tough to maintain.

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

- Advertisement -