Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

SQL Server – Alternative to using ‘Case When’ in Where clause of SQL Statement

I came across a scenario where I needed to write conditional statements in my WHERE clause of my SQL that was more complex than what a ‘CASE WHEN’ statement could provide.

I have two parameters, @FieldA and @FieldB. Both parameters can have a value of either ‘A’, ‘Y’, or ‘N’.

If the value = ‘A’, I want to basically bring back all records as if I am not using a filter at all as if to say where the column on the table >=0.

If the value = ‘Y’, I want to bring back rows where the column on the table > 0.

If the value = ‘N’, I want to bring back rows where the column on the table = 0.

Here is my table:

Running the following SELECT statements (without using the parameters mentioned above) brings back these result counts.

Now, I need to build one SQL statement using the the two parameters mentioned above to yield the same results.

Instead of using a ‘CASE WHEN’ statement, you simply examine your parameter in your WHERE clause, and depending on parameter value, you follow the evaluation with the ‘AND’ operand and the fully qualified filtered statement that includes the database column, the operator, and the value to compare.

SELECT FieldA, FieldB
FROM test
WHERE
(((@FieldA = 'Y' and Fielda > 0)
 OR (@FieldA = 'N' and Fielda = 0)
 OR (@FieldA = 'A' and Fielda >= 0))
 AND
 ((@FieldB = 'Y' and FieldB > 0)
 OR (@FieldB = 'N' and FieldB = 0)
 OR (@FieldB = 'A' and FieldB >= 0)))

As you can see from the above statement, the outcome of the WHERE clause would look something like this:

SELECT FieldA, FieldB
FROM test
WHERE Fielda ? 0 AND FieldB ? 0

The ? would be translated to be either ‘>’,   ‘=’,   or   ‘>=’   depending on the value of your parameters.

So as you can see, ‘CASE WHEN’ can be very limited where the above solution allows you to fully qualify your conditional statements.

Leave a Reply