Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Samuel Nitsche
Samuel Nitsche

Posted on • Originally published atdeveloper-sam.de on

Dead easy NULL-aware comparison in Oracle with DECODE

One of the probably most unnerving things in databases is dealing with NULLs, especially when comparing data (e.g. looking for some data with the use of other data where you can’t be sure if either of them will be NULL).

Consider the following data:

NAMEAGE
Chewbacca86
NULLNULL

If we would join that data with itself and compare the names, we would expect the following result:

NAME1NAME2NAMES_MATCH
ChewbaccaNULLnot equal
ChewbaccaChewbaccaequal
NULLNULLequal
NULLChewbaccanot equal

This is achievable by the following SQL statement, using CASE…WHEN:

withtest_dataas(select'Chewbacca'wookie_name,86agefromdualunionallselectnull,nullfromdual)selecttd1.wookie_namewookie_name1,td2.wookie_namewookie_name2,casewhen(td1.wookie_nameisnotnullandtd2.wookie_nameisnotnullandtd1.wookie_name=td2.wookie_name)or(td1.wookie_nameisnullandtd2.wookie_nameisnull)then'equal'else'not equal'endnames_matchfromtest_datatd1crossjointest_datatd2
Enter fullscreen modeExit fullscreen mode

Pretty verbose and not exactly pretty. We can, however, remove theand td2.wookie_name is not null part, because if NAME1 is not NULL, a comparison with NULL will always fail.

But still, the comparison is very verbose and it’s a lot to type.

If we are on Oracle however, we can remove that whole CASE statement by one simple DECODE call:

withtest_dataas(select'Chewbacca'wookie_name,86agefromdualunionallselectnull,nullfromdual)selecttd1.wookie_namewookie_name1,td2.wookie_namewookie_name2,decode(td1.wookie_name,td2.wookie_name,'equal','not equal')names_matchfromtest_datatd1crossjointest_datatd2
Enter fullscreen modeExit fullscreen mode

And yes, it works for all data types:

withtest_dataas(select'Chewbacca'wookie_name,86agefromdualunionallselectnull,nullfromdual)selecttd1.ageage1,td2.ageage2,decode(td1.age,td2.age,'equal','not equal')age_matchfromtest_datatd1crossjointest_datatd2
Enter fullscreen modeExit fullscreen mode

And yes, it can be easily used in the where clause:

withtest_dataas(select'Chewbacca'wookie_name,86agefromdualunionallselectnull,nullfromdual)selecttd1.wookie_namewookie_name1,td2.wookie_namewookie_name2fromtest_datatd1crossjointest_datatd2wheredecode(td1.wookie_name,td2.wookie_name,1,0)=1
Enter fullscreen modeExit fullscreen mode

So the next time you are comparing values that could be NULL and want to write a complicated CASE…WHEN statement – think about your friend DECODE and let it do its magic!

(Thank you very muchJacek Gebal for showing me this little trick)

The postDead easy NULL-aware comparison in Oracle with DECODE appeared first onDeveloper Sam.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Curiosity-driven software-developer, 10x underpants.Striving for harm-reduction.We don't need more rockstars, we need more mentors.
  • Location
    Germany
  • Work
    Software Developer at Smart Enterprise Solutions GmbH
  • Joined

More fromSamuel Nitsche

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp