Groups | Blog | Home
all groups > sql server (microsoft) > march 2005 >

sql server (microsoft) : Which condition matced


Jinsong
3/21/2005 11:31:57 AM
I have a selected statement like following

SELECT * FROM atable WHERE COLUMN1='aaa' OR COLUMN2='bbb'

Is there any way I can added a indictor to tell me a record returned
because which condition (COLUMN1='aaa' OR COLUMN2='bbb') matched? The
reason is I will have to use the return records differently based on
which condition matched. And the table has more the million records; I
don't really want to separate the statement to two.
Madhivanan
3/21/2005 9:17:50 PM
Try this

SELECT *,'aaa' as MatchedCol FROM atable WHERE COLUMN1='aaa'
Union
SELECT *,'bbb' as MatchedCol FROM atable WHERE COLUMN2='bbb'

Madhivanan
sienko
3/22/2005 12:57:05 AM
Select *, (Case When Column1='aaa' then 1 else 0 end) as Col1_Match,
(Case When Column2='bbb' then 1 else 0 end) as Col2_Match
From.....

The calculated column Col1_Match will be 1 if the condition for column
1 is met or 0 otherwise. The same applies to Col2_Match with respect to
Column2.

Note that it is possible for both conditions to be met in which case
both calculated columns will have 1.
AddThis Social Bookmark Button