[quoted text, click to view] "Sugapablo" <russREMOVE@sugapablo.com> wrote in message
news:slrnc0tk5e.nn4.russREMOVE@dell.sugapablo.net...
> I have column data that looks like this:
>
> id | ColumnA
> ---+-----------
> 1 | 12,35,123
> 2 | 1,23
> 3 | 233,34,35
> 4 | 34
>
> And I want to be able to make a WHERE clause where I can match up a
> value to one of the values between the commas. I.e. each of the numbers
> between the commas are seperate values I wish to compare against.
>
> Is there any function, perhaps like VBScripts "Split" function, where I
> can split those numbers up and compare a value against them?
>
> Perhaps there's another answer someone has?
>
> Example:
> If I'm searching with a value of "12" row 1 would be returned.
> If I'm searching with a value of "35", row 1 and 3 would be returned.
>
> --
> [ Sugapablo ]
> [
http://www.sugapablo.com <--music ]
> [
http://www.sugapablo.net <--personal ]
> [ sugapablo@12jabber.com <--jabber IM ]
There may well be a better way, but this should work (the string functions
in MSSQL are rather limited):
/* With a constant */
select *
from foo
where cola like '35,%' or
cola like '%,35,%' or
cola like '%,35'
/* With a variable */
declare @i int
set @i = 35
select *
from foo
where cola like cast(@i as varchar(5)) + ',%' or
cola like '%,' + cast(@i as varchar(5)) + ',%' or
cola like '%,' + cast(@i as varchar(5))
This will be inefficient on large data sets, though, and you may want to
look at your data model - lists in a single column can often be normalized.
Simon