Follow @endyourif rss Facebook LinkedIn

MSSQL Running an extra AND statement

Published on Jan 24, 2020

This had me baffled and shook me to the core as it went against everything I understand about an AND statement. I was using a standard CASE as I have done hundreds, if not thousands, of times that contained two AND statements. The first part of the statement returned false but, MSSQL still proceeded to execute the second statement. This may not seem like a big deal, but in this particular case I had an error producing data type mismatch problem. When the first part of the CASE was true the second part compared the data as a bigint whereas the actual value was an nvarchar, so it errored.

Let's take a look at an example.

A practical CASE Statement Example

Published on Mar 4, 2009

What is a SQL case statement? Well, it's much like a switch statement in a development language. It allows you to test several different cases for a field and determine what you would like to do for each one.

If you have ever visited Mysql's documentation, you may have been as lost as I was. You will find something like this:

CASE v

WHEN 2 THEN SELECT v;

WHEN 3 THEN SELECT 0;

ELSE

BEGIN

END;

END CASE;



If you are as confused as I was, let me provide you a better, more clear example.