COALESCE statement in SQL Server
This article will show you in detail how to use the COALESCE statement handling function in SQL Server with specific syntax and examples to better visualize and capture functions.
Describe
The COALESCE statement returns the expression with the first non-NULL value among the passed expressions. If all expressions are NULL then COALESCE will return NULL.
Syntax
To use the COALESCE statement in SQL Server, we use the following syntax:
COALESCE (bieuthuc_1, bieuthuc_2,. bieuthuc_n)
Parameters :
- bieuthuc_1, bieuthuc_2, . bieuthuc_n : is an expression of any type. All of these expressions must be in the same form or convert to the same form. For example, bieuthuc_1 is a string type, bieuthuc_2 to bieuthuc_n must also be a string type, similar to bieuthuc_1 is a numeric type then bieuthuc_2 to bieuthuc_n must also be numeric type .
Note :
- COALESCE can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.
For example
Take a look and explore some examples of COALESCE statements in SQL Server.
SELECT COALESCE(NULL, NULL, 'TipsMake.com', NULL, 'ChuyenCongNghe');
Result: 'TipsMake.com'
SELECT COALESCE(NULL, 'TipsMake.com', 'Quantrimang');
Result: 'TipsMake.com'
SELECT COALESCE(NULL, NULL, 1, 2, 3, NULL, 4);
Result: 1
Previous post: CASE command in SQL Server
Next lesson: CURRENT_USER command in SQL Server
4.7 ★ | 3 Vote
You should read it
May be interested
- NULLIF function in SQL Serverthis article will show you in detail how to use the nullif function handler in sql server with specific syntax and examples to better visualize and capture functions.
- The difference between Truncate and Delete in Microsoft SQL Serverin the following article, we will help you distinguish some basic differences between two delete syntax and truncate table in microsoft sql server application. basically, both of these statements help us to remove the data, but in essence it is not so.
- ISNULL function in SQL Serverin sql server, the isnull function allows you to return an alternate value when an input expression is null.
- GROUP BY clause in SQL Serverthe group by clause in sql server (transact-sql) is used in the select statement to retrieve data from multiple records and result groups into 1 or more columns.
- ISNUMERIC function in SQL Serverthe isnumeric function in sql server checks whether the value of the passed expression is a valid numeric value, if there is isnumeric returns 1, otherwise it returns 0.
- The difference between web server and app serveryou have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
- JOIN in SQL Serverjoin is used to retrieve data from multiple tables, occurring when two or more tables are connected together in an sql statement.
- BREAK (Control Interrupt) command in SQL Serverthe break command used to exit the loop does not specify a stop condition or you want to stop the loop on condition that you specify and execute the statements following the loop statement end.
- CREATE TABLE command in SQL Serverin sql server (transact-sql), the create table statement is used to create and define tables.
- If ... Else in Cwhat is the if … else condition in c? what is the meaning and usage of if else statement in c? let's find out with tipsmake.com.com!