tag:blogger.com,1999:blog-7205115418491271533.post5596779605333309141..comments2015-01-07T13:58:11.175-08:00Comments on SelectSql: Coalesce in Sql server exampleVijay Mauryahttp://www.blogger.com/profile/00459112937519229095noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-7205115418491271533.post-76114463903678877542012-07-24T00:59:37.140-07:002012-07-24T00:59:37.140-07:00A more efficient approach to creating dynamic WHER...A more efficient approach to creating dynamic WHERE clauses involves using the COALESCE function. This function returns the first non-null expression in its expression list. The following example shows how it works.<br /><br />DECLARE @Exp1 varchar(30),<br /> @Exp2 varchar(30)<br /><br />SET @Exp1 = NULL<br />SET @Exp2 = 'SQL Server'<br /><br />SELECT COALESCE(@Exp1,@Exp2)<br /><br />--Results--<br /><br />------------------------------<br />SQL Server<br />The function processes the expression list from left-to-right and returns the first non-null value. The COALESCE function can process an infinite number of expressions (e.g., COALESCE(@Exp1,@Exp2,@Exp3,@Exp4,...)), but for the example presented in this article only two are needed.Vijay Mauryahttps://www.blogger.com/profile/00459112937519229095noreply@blogger.com