SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get rows containing value in comma seprated
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pushp82
Yak Posting Veteran

67 Posts

Posted - 05/24/2012 :  05:40:54  Show Profile  Reply with Quote
Hi Friends,

I have a table like:
ID VALUES
1 15,14,10
2 4,3,2,1
3 5,6,3,2
-- -----
-- -----

How to select ID which has 2 in VALUES column?

Please try to solve it ASAP as I'm just stuck and watching my monitor for solution.

Thanks in advance.
Pushp

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/24/2012 :  05:56:38  Show Profile  Reply with Quote

where ',' + VALUES + ',' LIKE '%,2,%'



KH
Time is always against us

Go to Top of Page

pushp82
Yak Posting Veteran

67 Posts

Posted - 05/24/2012 :  06:26:33  Show Profile  Reply with Quote
this will return values for 12 22 as well I need exact actually

ohhh sorry i did not noticed "," after %
thanks
please help one step more how to pass 2 as parameter?
and thanks for this help
quote:
Originally posted by khtan


where ',' + VALUES + ',' LIKE '%,2,%'



KH
Time is always against us




Edited by - pushp82 on 05/24/2012 06:30:38
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/24/2012 :  06:30:55  Show Profile  Reply with Quote

declare @tbl table
(
	[ID]	int,
	[VALUES]	varchar(10)
)

insert into @tbl select 1, '15,2,14,10'
insert into @tbl select 2, '12,13,14'
insert into @tbl select 3, '21,22,23'

select	*
from	@tbl
where	',' + [VALUES] + ',' like '%,2,%'

/* RESULT
ID          VALUES     
----------- ---------- 
1           15,2,14,10

(1 row(s) affected)
*/



KH
Time is always against us

Go to Top of Page

pushp82
Yak Posting Veteran

67 Posts

Posted - 05/24/2012 :  06:49:12  Show Profile  Reply with Quote
Ok I was able to do it as :

declare @i int
set @i=2
select * from TABLE where VALUES
LIKE '%,'+cast(@i as varchar) +',%' or VALUES LIKE cast(@i as varchar) +',%'
or VALUES LIKE '%,'+cast(@i as varchar)

thanks

Edited by - pushp82 on 05/24/2012 06:51:03
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000