Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Chapter 3 - Video # 2

Working with NULLs in


Expressions

Chapter 3: Using and Understanding Scalar Functions and Data Types


Course: Writing Queries for SQL Server (2005/2008 Edition)
Course Id: 160
Presented by Scott Whigham

1
p. 1
Before we get started…

• All functions in SQL:


– Require parentheses
– Accept 0 or more arguments (some of which may
be optional)

p. 2
Before we get started…

• Scalar functions:
– Can be used anywhere an expression can be used
• All clauses and with all operators
– Can be nested

p. 3
NULLs in Expressions

• In Chapter 2 we talked about deal with


NULLs in predicates
– IS NULL returns true if a predicate evaluates to
NULL
– IS NOT NULL returns true if a predicate evaluates
to a non-NULL value

p. 4
NULLs in Expressions

• Let’s now talk about dealing with NULLs


in expressions
– Reminder: the result of any expression between a
NULL and a non-NULL (i.e. an “unknown” and a
“known”) returns NULL

SELECT 1 + NULL

– “What is 1 plus an unknown number?”


• “Unknown!”

p. 5
NULLs in Expressions

• For the examples in this video, here is


our sample set of data:

p. 6
NULLs in Expressions

“Give me a list of all customers and put their entire


name into one column”

p. 7
NULLs in Expressions

• Often we want to ignore the unknowns


and simply return the known values:

p. 8
NULLs in Expressions

• To handle NULLs in expressions, use:


– ISNULL() converts NULL inputs to known outputs
• Transact-SQL specific
– COALESCE() converts NULL inputs to known
outputs
• ISO standard
– NULLIF() converts known inputs to NULLs based
on predicate
• ISO standard

p. 9
Next up

• Introduction to NULLIF, ISNULL, and


COALESCE

“Database management would be simpler if


missing values didn't exist."
E.F. Codd

p. 10

You might also like