Suppose you want to label the films by their length based on the following logic: Let’s take a look at the film table from the sample database. If you omit the ELSE clause, the CASE expression returns NULL. In case all conditions evaluate to false, the CASE expression returns the result ( else_result) that follows the ELSE keyword. Also, it immediately stops evaluating the next expression. For example, if the condition_2 evaluates to true, the CASE expression returns the result_2. If a condition evaluates to true, the CASE expression returns the corresponding result that follows the condition. When a condition evaluates to false, the CASE expression evaluates the next condition from the top to bottom until it finds a condition that evaluates to true. ![]() In this syntax, each condition ( condition_1, condition_2…) is a boolean expression that returns either true or false. ![]() The following illustrates the general form of the CASE statement: CASEĮND Code language: SQL (Structured Query Language) ( sql ) The CASE expression has two forms: general and simple form. ![]() Since CASE is an expression, you can use it in any places where an expression can be used e.g., SELECT, WHERE, GROUP BY, and HAVING clause. It allows you to add if-else logic to the query to form a powerful query. The PostgreSQL CASE expression is the same as IF/ELSE statement in other programming languages. Summary: in this tutorial, you will learn how to use the PostgreSQL CASE conditional expression to form conditional queries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |