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)
 help with sql query

Author  Topic 

dave914
Starting Member

2 Posts

Posted - 2005-11-30 : 17:03:19
Hi, I need to search a column in my database (varchar:50) that contains a comma delimited string of numbers (i.e. 1, 2, 3, 4, 5, 10). Currently, I am doing the following SQL query:

SELECT * FROM people WHERE clubs_belongs_to LIKE '%1%'

Where 1 is the number I'm searching for. The problem with the query above is that it returns records that contain 1, 10, 11, 12, 13, etc. in the clubs_belongs_to field. I want the query to only return those people who belong to club number 1, not 10, 11, 12, etc. Please help. Thanks in advance. I have tried using IN instead of LIKE, but that didn't seem to get the results I wanted either.

sbhegel
Starting Member

19 Posts

Posted - 2005-11-30 : 17:12:56
What if you don't use the %1% but just LIKE 1

Scott
Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-11-30 : 19:04:05
If I understand this correctly, I don't believe using LIKE 1 will work. If your data is uniform (i.e., the numbers are always separated by a space between the comma and the next number), then you can perhaps use this query:

SELECT * FROM people
WHERE clubs_belongs_to LIKE '1,%'
OR clubs_belongs_to LIKE '% 1,%'
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-01 : 03:16:37
or Like '1' , ='1'
Can you post some sample data and the result you want?

Madhivanan

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

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-01 : 03:20:53
SELECT * FROM people
WHERE ','+clubs_belongs_to+',' LIKE '%,1,%'
Go to Top of Page
   

- Advertisement -