SQL help

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
I need to search a string of text for where any of three 2 letter codes appear more than once.

Example if codes were "AB", "CD" & "EF", these could appear in any order in one string.

I would want it to pick up this: "CD - blah blah blah. EF - blah blah. AB - blah"
this: "EF - blah blah. CD - blah"

but not this: "EF - blah"

Any ideas, sorry it's not really that clear.

Cheers,
 
Code:
SELECT * FROM Table WHERE (Field LIKE '%AB%' OR Field LIKE '%CD%' OR Field LIKE '%EF%') AND Field <> 'EF - blah';

Thanks for the reply, but that wouldn't work. The single EF could be a single anything (i.e. it could be "AB - blah" or "CD - blah" or "EF - blah".) and it shouldn't pick up anything as long as there is only one of the codes.

In your example, it would pick up the string if it had only one of the codes in the string.

Thanks for the help.
 
Code:
SELECT * 
FROM x 
WHERE 
(CASE WHEN CHARINDEX(Field, 'AB') > 0 THEN 1 ELSE 0 END 
+ CASE WHEN CHARINDEX(Field,'CD') > 0 THEN 1 ELSE 0 END
+ CASE WHEN CHARINDEX(Field,'EF') > 0 THEN 1 ELSE 0 END) = 3

might do it - not sure of CHARINDEX order of parameters and not in front of SQL help
 
Last edited:
Back
Top Bottom