SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need a Query where with case statement


Need a Query where with case statement

Author
Message
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
This Query is not working please help........

select * from tbl e
INNER JOIN LKUP L ON e.ID=L.LKUP_ID
WHERE
CASE WHEN ISNULL(DATEPART(YY,(SU_DT)),0)=2014 OR ISNULL(DATEPART(mm,(SU_DT)),0)>=11
THEN (LKUP_VAL_EN LIKE('V2%') OR LKUP_VAL_EN LIKE('V3%'))
ELSE LKUP_VAL_EN LIKE('V2%')
END
AND e.Active=1
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16468 Visits: 19557
Sometimes it's easier to work with the expression in an APPLY block, dropping the results down to the WHERE clause like this:
SELECT e.*, 
x.Datefilter,
x.LookupFilter
FROM tbl e
INNER JOIN LKUP L
ON e.ID = L.LKUP_ID
CROSS APPLY (
SELECT
Datefilter = CASE
WHEN YEAR(SU_DT) = 2014
OR MONTH(SU_DT) >= 11 THEN 1
ELSE 0 END,
LookupFilter = CASE
WHEN LKUP_VAL_EN LIKE 'V2%' THEN 1
WHEN LKUP_VAL_EN LIKE 'V3%' THEN 2
ELSE 0 END
) x
WHERE e.Active = 1
AND (
(x.Datefilter = 1 AND x.LookupFilter = 1)
OR (x.Datefilter = 0 AND x.LookupFilter = 2)
)



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search