Click here to Skip to main content
15,891,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if i have a table like this
id  |  name  |  values 
1       abc     1,2,3,4,5,6
2       abc1     1,2,3,4,5,6,7,8,9
3       abc2     1,2,3,4,5,6,7,10,12,13,45
4       abc     1,2,3,4,5,6,15,16,45
5       abc3     1,2,3,4,5,6,7,8

i have to select those record according to @variable value='7' in the values field columns of all records and how to split the values with commas
for example select * from table name where '7' lies in values
Posted
Updated 19-Nov-14 22:48pm
v2

SQL
SELECT 
     *
FROM 
    YOURTABLE
WHERE
      values LIKE '%,' + @variable + ',%' --middle
      OR
      values LIKE @variable + ',%' --start
      OR
      values LIKE '%,' + @variable --end
      OR 
      values =  @variable --single 


Or shorter version of above
SQL
SELECT
     *
FROM
    YOURTABLE
WHERE  (',' + RTRIM(values) + ',') LIKE '%,' + @variable + ',%'

refer : http://stackoverflow.com/questions/5611715/where-value-in-column-containing-comma-delimited-values[^]
 
Share this answer
 
v2
Try this

SQL
select * from tablename where charindex(',7,',','+values+',')>0
 
Share this answer
 
v3
Comments
DamithSL 20-Nov-14 5:01am    
this will fail, values = "1,17" also select
Shweta N Mishra 20-Nov-14 5:05am    
ok correct , it can be improved like this

select * from tablename where charindex(',7,',','+values+',' )>0
select * from tablename where values like '%,7,%'
 
Share this answer
 
It may help you,try this.
SQL
Declare @key varchar(1000)
set @key= '7'
SELECT * FROM tablename
WHERE CHARINDEX( ',' + @key + ',', ',' + columnname+ ',' ) > 0 ;
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900