Solved

SQL CASE inside of AND

Posted on 2011-09-21
5
169 Views
Last Modified: 2012-05-12
I need to embed a case statement inside of an AND statement.

Basically, if company1 is fed in, I want records that have an new_employeridname value of company1 or company2 in my results.

Otherwise (company3, company4, etc) I just want the records that directly match.

See last line below.

Thanks.
SELECT new_planname, new_benefitplanid, new_provider
FROM new_benefitplan WITH(NOLOCK)
WHERE new_planyear = @Year
AND new_inscope = 1
AND new_benefitplantype = 1
AND statuscode <> 2
AND CASE WHEN @EmployerIdName = 'Company1' THEN ( new_employeridname = @EmployerIdName OR new_employeridname = 'Company2')ELSE new_employeridname = @EmployerIdName END

Open in new window

0
Comment
Question by:sailing_12
5 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 300 total points
ID: 36577100
Hi,

If I understand correctly, this is what you want.

/peter

SELECT new_planname, new_benefitplanid, new_provider
FROM new_benefitplan WITH(NOLOCK)
WHERE new_planyear = @Year
AND new_inscope = 1
AND new_benefitplantype = 1
AND statuscode <> 2
AND (new_employeridname = @EmployerIdName OR (@EmployerIdName = 'Company1' AND new_employeridname = 'Company2'))

Open in new window



0
 
LVL 18

Expert Comment

by:lludden
ID: 36577128
Not real sure, but this looks like it might be what you wanted:

SELECT new_planname, new_benefitplanid, new_provider
FROM new_benefitplan WITH(NOLOCK)
WHERE new_planyear = @Year
AND new_inscope = 1
AND new_benefitplantype = 1
AND statuscode <> 2
AND new_employeridname = CASE WHEN @EmployerIdName IN('Company1','Company2') THEN new_employeridname ELSE @EmployerIdName END
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36577999
like this ?


SELECT new_planname, new_benefitplanid, new_provider
FROM new_benefitplan WITH(NOLOCK)
WHERE new_planyear = @Year
AND new_inscope = 1
AND new_benefitplantype = 1
AND statuscode <> 2
AND new_employeridname in (@employeridname,case @employeridname when 'company1' then 'company2' end)

Open in new window

0
 
LVL 2

Expert Comment

by:smu95rp
ID: 36579385
You don't really want to use CASE for that, just an OR condition in the WHERE clause:-

Please note the parentheses.
SELECT new_planname, new_benefitplanid, new_provider
FROM new_benefitplan WITH(NOLOCK)
WHERE new_planyear = @Year
AND new_inscope = 1
AND new_benefitplantype = 1
AND statuscode <> 2
AND ( @EmployerIdName = 'Company1' And new_employeridname In ('Company1', 'Company2')
    OR @EmployerIdName Not In ('Company1', 'Company2') And @EmployerIdName = new_employeridname )

Open in new window

0
 

Author Closing Comment

by:sailing_12
ID: 36581295
Looks good. Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 37
using t-sql EXISTS 8 23
Pivot not using aggregate yield error 3 22
SqlAdvisor 2016 3 11
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now