Blog Post

T-SQL COALESCE Example

,

T-SQL COALESCE

SQL Server T-SQL coalesce simplifies the use of a case statement to find the first non-null value of your expression.  For example, I want to return the products in the AdventureWorks sample database and show the SellEndDate if it exists or the SellStartDate if the SellEndDate is null.

Example Script:

SELECT

[ProductID]

,[Name]

,[SellStartDate]

,[SellEndDate]

,coalesce([SellEndDate],[SellStartDate]) as Sell_Date_End

FROM [AdventureWorks2014].[Production].[Product]

where ProductID >720

Results:

TSQL COALESCE

As you can see in the above image, row 5 populates the Sell_Date_End column with 5/29/2013 since the SellEndDate is not null.  Otherwise, if the SellEndDate is null the SellStartDate is populated.

 

For complete information on Coalesce see MSDN

The post T-SQL COALESCE Example appeared first on BI and Predictive Analytics.

Rate

Share

Share

Rate