COALESCE Sample


-- Coalesce take Comma seprated Fields, it will Take First Non NULL Field Value

Select Coalesce(Afld,BFld,CFld)  as NFld, Afld,BFld,CFld From
(
Select 'A' as AFld,'B' as BFld, 'C' as CFld
Union all
Select NULL as AFld,'B' as BFld, 'C' as CFld
Union all
Select NULL as AFld,NULL as BFld, 'C' as CFld
) Alldata


-- Writting Same Logic with out Coalesce using Case when

Select Case when AFld is not null then AFld Else Case when Bfld Is not null then BFld Else Case when Cfld IS not null then Cfld End End End As Nfld
, Afld,BFld,CFld From
(
Select 'A' as AFld,'B' as BFld, 'C' as CFld
Union all
Select NULL as AFld,'B' as BFld, 'C' as CFld
Union all
Select NULL as AFld,NULL as BFld, 'C' as CFld
) Alldata



Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)