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
 General SQL Server Forums
 New to SQL Server Programming
 Select Query where value in array

Author  Topic 

mitkarker
Starting Member

3 Posts

Posted - 2009-08-12 : 11:17:15
Hello everyone.

Here is what I am trying to get.
I have a DB (named "jobs) and in it a column (named "projects") that stores string array of numbers separated by commas.

Example:
in row 1, "projects" is "5"
in row 2, "projects" is "1,5,4"
in row 3, "projects" is "3,6,50"

I want to select the rows in that DB where "5" is inside the string array.
Now, when I used the "IN" like this:
SELECT * from jobs WHERE '5' IN (projects)
I get just row 1.
When I use the "LIKE" like this:
SELECT * from jobs WHERE projects LIKE '%5%'
I get rows 1,2,3

What I really really really really really really want is to get rows 1,2

I've been searching for 2 days now. I found that FinD_in_SET is for MySQL but I use SQL Server, and tried all kind of commands. I actually think I got a little more knowledge now :)... but still not what I want.

Thanks a bunch!

WoodHouse
Posting Yak Master

211 Posts

Posted - 2009-08-12 : 11:20:45
quote:
Originally posted by mitkarker

Hello everyone.

Here is what I am trying to get.
I have a DB (named "jobs) and in it a column (named "projects") that stores string array of numbers separated by commas.

Thanks a bunch!



Hi

I think you have a table(named "jobs) right..
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-12 : 11:53:12
mitkarker.

your long search is over!

Go here for all the information you will probably ever need about implementing arrays in SQL server.

http://www.sommarskog.se/arrays-in-sql-2005.html

All the best.

I'd read that article and then if you have any specific problems, post them here. You'll get some suggestions.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

mitkarker
Starting Member

3 Posts

Posted - 2009-08-12 : 12:06:39
quote:
Originally posted by WoodHouse

quote:
Originally posted by mitkarker

Hello everyone.

Here is what I am trying to get.
I have a DB (named "jobs) and in it a column (named "projects") that stores string array of numbers separated by commas.

Thanks a bunch!



Hi

I think you have a table(named "jobs) right..




Yeah.... :) A table.
Go to Top of Page

mitkarker
Starting Member

3 Posts

Posted - 2009-08-12 : 12:38:26
Well, found an easy way to bypass the problem:

SELECT * from jobs WHERE projects LIKE '%5%' AND projects NOT LIKE '%5[0-9]%'

It always happens: once you post, you find an answer a little after. Next time I'll post immediately. LOL

Thanks repliers!
Go to Top of Page
   

- Advertisement -