Tutorial by Examples

We can get a comma delimited string from multiple rows using coalesce as shown below. Since table variable is used, we need to execute whole query once. So to make easy to understand, I have added BEGIN and END block. BEGIN --Table variable declaration to store sample records DECLARE @...
COALESCE() returns the first NON NULL value in a list of arguments. Suppose we had a table containing phone numbers, and cell phone numbers and wanted to return only one for each user. In order to only obtain one, we can get the first NON NULL value. DECLARE @Table TABLE (UserID int, PhoneNumber va...
SELECT COALESCE(NULL, NULL, 'TechOnTheNet.com', NULL, 'CheckYourMath.com'); Result: 'TechOnTheNet.com' SELECT COALESCE(NULL, 'TechOnTheNet.com', 'CheckYourMath.com'); Result: 'TechOnTheNet.com' SELECT COALESCE(NULL, NULL, 1, 2, 3, NULL, 4); Result: 1

Page 1 of 1