Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

CASE in WHERE statement

Avatar of Mark Wilson
Mark Wilson asked on
Microsoft SQL Server
6 Comments1 Solution361 ViewsLast Modified:
I have developed the following query below


DECLARE @start DATETIME
DECLARE @end DATETIME
DECLARE @team varchar(25)

SET @start = DATEADD(day, 1 - DATEPART(day, GETDATE()),
                     CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
SET @start = DATEADD(month, -1, @start)
SET @end = DATEADD(day, -1, DATEADD(month, 1, @start)) + 1

SELECT  etc
FROM    etc
WHERE   CASE WHEN I.kbxreference LIKE 'LP%' THEN 'Law Pack'
             WHEN I.kbxreference LIKE 'TG%' THEN 'Telegraph'
             WHEN I.kbxreference LIKE 'JL%' THEN 'John Lewis'
             WHEN I.kbxreference LIKE 'IM%' THEN 'IM Wills Online'
             ELSE ws.name
        END = @team
        AND   CASE WHEN ws.worksourceid IN (211,212) THEN a.engrossed >= @start
        and a.engrossed < @end else m.created > @start and m.created < @end end

I am having problems with the following

 AND   CASE WHEN ws.worksourceid IN (211,212) THEN a.engrossed >= @start
        and a.engrossed < @end else m.created > @start and m.created < @end end

Is there any other way I can rewrite it to make it work?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of G Trurab Khan
G Trurab KhanFlag of Pakistan imageSnr. Development Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers