-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworking_with_nulls.sql
More file actions
39 lines (30 loc) · 912 Bytes
/
Copy pathworking_with_nulls.sql
File metadata and controls
39 lines (30 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- Working with NULLS
-- NULL numbers = 0
SELECT name, ISNULL(TRY_CAST(size as INT), 0) AS NumericSize
FROM SalesLt.Product;
-- NULL strings = blank string
SELECT productnumber, ISNULL(color, '') + ', ' + ISNULL(size, '') AS productdetails
FROM SalesLt.Product;
-- Multi color = NULL
SELECT name, color, NULLIF(color, 'Multi') AS singlecolor
FROM SalesLt.Product;
-- find first non-null date
SELECT name,discontinueddate, sellenddate, sellstartdate, COALESCE(discontinueddate, sellenddate, sellstartdate) AS lastactivity
FROM SalesLt.Product;
-- Searched CASE
SELECT name,
CASE
WHEN sellenddate IS NULL THEN 'On Sale'
ELSE 'Discontinued'
END AS salesstatus
FROM salesLT.Product;
-- Simple CASE
SELECT name, size,
CASE size
WHEN 'S' THEN 'Small'
WHEN 'M' THEN 'Medium'
WHEN 'L' THEN 'Large'
WHEN 'XL' THEN 'Extra Large'
ELSE ISNULL(size, 'n/a')
END AS productsize
FROM saleslt.product;