


SQL:2003 has introduced two more abbreviations that extend the simple case. The above shown forms of case were all introduced with intermediate SQL-92 and work in practically all SQL databases. Buying my book “SQL Performance Explained” (from €9.95) also supports my work on this website. I offer SQL training, tuning and consulting. Nullif is also defined as a transformation to case and is typically used to prevent division by zero errors: x / NULLIF(y, 0) x / CASE WHEN y = 0 THEN null Only if both values are equal ( =), null is returned instead. Nullif requires two parameters and generally returns the value of the first one. The following expressions are therefore equivalent: COALESCE(a, b) CASE WHEN a IS NOT NULL THEN a The standard defines coalesce as a transformation into a case expression. Both are used like functions and do not use the keywords case, when, then, else and end.Ĭoalesce returns the first not- null parameter (or null, if all parameters are null). SQL offers two case abbreviations to cope with null: coalesce and nullif. That is because the condition null = null is not true 5-consequently, a when null clause never applies. You cannot use simple case to test for null because it always uses the equals operator ( =). You’ll find more about this in Proprietary Extensions: Documented Evaluation Process.
#Postgresql if then else how to
Ultimately, this is a result of the declarative nature of SQL: the database-not the programmer-decides how to get the correct result. Of course, these products eventually deliver the right result-they just discard the unnecessarily evaluated ones. 3 In fact, there are products that sometimes process a then clause even before the corresponding when clause (read it again: then before when). The SQL standard does not specify how to process case expressions-it just defines the result. 1 If else is omitted, else null is implicit. īefore closing the case expression with end, the optional else clause can be used. 0 Put simply: case expressions evaluate to the of the first true. To close that gap, SQL uses the order in which the conditions appear in the case expression to define their precedence. A flat case expression does not have such an inherent precedence. On the other hand, nesting provides an inherent precedence among the conditions.

This allows for multiple conditions even without nesting-unlike the ?: operator in other programming languages. is an arbitrary SQL expression: it can be a simple expression like a constant or column name, or also a complex SQL expression containing subqueries and, of course, nested case expressions.Ī case expression can contain several when- then pairs. Note that is a condition like those in where clauses-for example: column_name IS NULL. From that point of view, one could say that searched case is the only case syntax you absolutely need to remember. Furthermore searched case works in virtually all SQL databases. The other variants are abbreviations that could also be written as searched case. The following example shows the syntax in the most generic form-the so-called searched case. The syntax of the case expression is very flexible and allows for some abbreviations. Its use is similar to the ternary operator ?: in other programming languages. Being an expression-rather than a control structure-means that case varies the result of formulas (expressions) based on conditions. Nevertheless, SQL has something similar: the case expression. SQL is a declarative language: it does not provide control over program flow like if does for imperative programs.
