![]() ![]() Posts Code language: SQL (Structured Query Language) ( sql ) PostgreSQL COALESCE example To achieve this, we can use the COALESCE function as follows: SELECT COALESCE (excerpt, LEFT( CONTENT, 150)) For example, we want to display the excerpt from a blog post, if the excerpt is not provided, we can use the first 150 characters of the of the content of the post. We often use the COLAESCE function to substitute a default value for null values when we querying the data. ![]() See the following examples: SELECT COALESCE ( 1, 2) Code language: SQL (Structured Query Language) ( sql ) SELECT COALESCE ( NULL, 2, 1) Code language: SQL (Structured Query Language) ( sql ) MySQL has IFNULL function, while Oracle provides NVL function. The COALESCE function provides the same functionality as NVL or IFNULL function provided by SQL-standard. All the remaining arguments from the first non-null argument are not evaluated. The COALESCE function evaluates arguments from left to right until it finds the first non-null argument. If all arguments are null, the COALESCE function will return null. It returns the first argument that is not null. The COALESCE function accepts an unlimited number of arguments. The syntax of the COALESCE function is as follows: COALESCE (argument_1, argument_2, …) Code language: SQL (Structured Query Language) ( sql ) You will learn how to apply this function in SELECT statement to handle null values effectively. Summary: in this tutorial, you will learn about the PostgreSQL COALESCE function that returns the first non-null argument. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |