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 2008 Forums
 Transact-SQL (2008)
 Select Query

Author  Topic 

rkumar
Starting Member

3 Posts

Posted - 2010-09-06 : 02:04:32
Table


Id Name DivisionId
1 Rajeev 2,3,11
2 Bhashkar 1,5,7
3 Atul 12,3,11


I wanted to search where divisionId is equal to '1'. Can you help me to solve this in single query. Please reply me if you have an idea[b]

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-09-06 : 02:18:16
[code]
select @divisionid = '1'
select *
from yourtable
where ',' + DivisionId + ',' like '%,' + @divisionid + ',%'[/code]

You should normalize your table.



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

rkumar
Starting Member

3 Posts

Posted - 2010-09-06 : 02:49:26
quote:
Originally posted by khtan


select @divisionid = '1'
select *
from yourtable
where ',' + DivisionId + ',' like '%,' + @divisionid + ',%'


You should normalize your table.



KH
[spoiler]Time is always against us[/spoiler]
Thanks It's working.
My database is normalize and I am searching on temporary tables created in procs.


Go to Top of Page

sanjayguptag
Starting Member

1 Post

Posted - 2010-09-06 : 07:20:49
Try to use following....

select *
from #tmp
where DivisionId like '1,%' and
( DivisionId not like '%[1-9]1' and
DivisionId not like '%1[1-9]' or
DivisionId like '%,1,%' or DivisionId like '%,1%' )
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-06 : 08:37:33
quote:
Originally posted by sanjayguptag

Try to use following....

select *
from #tmp
where DivisionId like '1,%' and
( DivisionId not like '%[1-9]1' and
DivisionId not like '%1[1-9]' or
DivisionId like '%,1,%' or DivisionId like '%,1%' )


Dont complicate it. See what khtan posted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -