Quantcast
Channel: Erik Darling – Brent Ozar Unlimited®
Viewing all articles
Browse latest Browse all 370

Getting Started With Oracle Week: NULLs and NULL handling

$
0
0

We’re not so different, you and I

In any database platform, you’ll have to deal with NULLs. They’re basically inescapable, even if you own an island. So let’s compare some of the ways they’re handled between Oracle and SQL Server.

Twofer

If you take a look at the two queries below, there are a couple things going on. First is the NVL function. It’s basically the equivalent of SQL Server’s ISNULL function, where it will return the second argument if the first is, well, NULL.

The second thing you may notice is the ORDER BY. In here we can do something really cool, and specify whether to put NULLs at the beginning, or end, of our results. SQL Server will just put them first, for better or worse. If you want to put them last, you need to do some dancing with the devil. Or just use a CASE expression in your ORDER by.

SELECT EMPLOYEE_ID, COMMISSION_PCT, NVL(COMMISSION_PCT, -1) AS "NULL_TEST"
FROM HR.EMPLOYEES
ORDER BY COMMISSION_PCT NULLS FIRST;

SELECT EMPLOYEE_ID, COMMISSION_PCT, NVL(COMMISSION_PCT, -1) AS "NULL_TEST"
FROM HR.EMPLOYEES
ORDER BY COMMISSION_PCT NULLS LAST;

I love stuff like this, because it gives you easy syntactic access to presentation goodies.

There’s another function, NVL2, which I haven’t quite figured out a lot of uses for, but whatever. It takes three arguments. If the first argument is NULL, it returns the third argument. If the first argument isn’t NULL, it returns the second argument.

SELECT EMPLOYEE_ID, COMMISSION_PCT, NVL2(COMMISSION_PCT, 1, 2) AS "NULL_TEST"
FROM HR.EMPLOYEES
ORDER BY COMMISSION_PCT NULLS FIRST;

The results end up something like this below.

I just learned how to do this, too.

I just learned how to do this, too.

There’s also NULLIF! Which does what you’d expect it to do: return a NULL if the two arguments match. Otherwise, it returns the first argument. Dodge those divide by zero errors like a pro.

SELECT 
NULLIF('RUMP', 'ERIK'),
NULLIF('ERIK', 'ERIK'),
NULLIF(1, -1),
NULLIF(1, 1)
FROM DUAL;
At long last, not a Rump

At long last, not a Rump

Last, but certainly not least, is the lovely and talented COALESCE. It’s a dead ringer for SQL Server’s implementation, as well.

SELECT
COALESCE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'HELLO!')
FROM DUAL;
Is it me you're looking for?

Is it me you’re looking for?

Intentionally left blank

NULLs happen to the best of us. Three-valued logic can be sneaky. I prefer to use canary values when possible. Those are values that could never naturally occur in data (think -999999999 or something). Again, this isn’t meant to be an exhaustive piece on NULLs and NULL handling, just a toe in the water for any SQL Server people who need to start working with Oracle.

Thanks for reading!

Wanna learn from us, but can't travel? Our in-person classes now have online dates, too.


Viewing all articles
Browse latest Browse all 370

Trending Articles