Solved

SQL CASE inside of AND

Posted on 2011-09-21
5
173 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

825 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