Click here to Skip to main content
13,202,706 members (53,879 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4K views
5 bookmarked
Posted 7 Jun 2017

SQL CASE WHEN condition THEN NumericColumn ELSE 0 END

, 7 Jun 2017
Rate this:
Please Sign up or sign in to vote.
Be careful with CASE WHEN when dealing with numeric columns and using zero literal

Recently I encountered a very interesting and hard to reproduce bug when using SQLite statement CASE WHEN. Many if not most SQL developers would consider the following (simplified) statement perfectly OK:

SELECT (CASE WHEN TableName.LogicCondition > 0 THEN TableName.NumericColumn ELSE 0 END) AS FilteredValue1, 
(CASE WHEN TableName.LogicCondition > 0 THEN 0 ELSE TableName.NumericColumn END) AS FilteredValue2 
FROM TableName WHERE TableName.SomeCondition = 1;

However, it is not OK at all at least if used in SQLite (haven't checked other SQL versions might also be affected).

The problem is the type of the FilteredValue columns returned by this query. If the first CASE WHEN execution ends with the result 0, the entire column type will be set as integer ('cause 0 is integer literal), which in turn leads to the truncation of fractional parts of the numeric column values. The first CASE WHEN hit depends on row order within the table and some specifics of the SQL engine, which leads to "random errors", i.e., very hard to reproduce.

The same holds for aggregation, e.g.: SUM(CASE WHEN TableName.LogicCondition > 0 THEN TableName.NumericColumn ELSE 0 END) sometimes will return an invalid result.

The solution - use 0.0 (numeric literal) instead of 0 (integer literal):

SELECT (CASE WHEN TableName.LogicCondition > 0 THEN TableName.NumericColumn ELSE 0.0 END) AS FilteredValue1,
(CASE WHEN TableName.LogicCondition > 0 THEN 0.0 ELSE TableName.NumericColumn END) AS FilteredValue2
FROM TableName WHERE TableName.SomeCondition = 1;

P.S.: I would be grateful if someone could check if the same problem occurs on other SQL implementations, especially MySQL.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Niemand25
Business Analyst Linden
Lithuania Lithuania
I'm a lawyer in a law firm. Programing is my hobby.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralSQL Server is an exception Pin
Suvendu Shekhar Giri8-Jun-17 21:22
mvpSuvendu Shekhar Giri8-Jun-17 21:22 
GeneralRe: SQL Server is an exception Pin
Don V Nielsen9-Jun-17 7:42
memberDon V Nielsen9-Jun-17 7:42 
GeneralRe: SQL Server is an exception Pin
Niemand2511-Jun-17 23:59
memberNiemand2511-Jun-17 23:59 
GeneralRe: SQL Server is an exception Pin
Member 1252223012-Jun-17 3:37
memberMember 1252223012-Jun-17 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 8 Jun 2017
Article Copyright 2017 by Niemand25
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid