Press "Enter" to skip to content

The World is CASE Expressions

Chad Baldwin re-enacts the astronaut meme:

Well…not really, but a handful of functions in T-SQL are simply just syntactic sugar for plain ol’ CASE statements and I thought it would be fun to talk about them for a bit because I remember being completely surprised when I learned this. I’ve also run into a couple weird scenarios directly because of this.

For those who don’t know what the term “syntactic sugar” means…It’s just a nerdy way to say that the language feature you’re using is simply a shortcut for another typically longer and more complicated way of writing that same code and it’s not unique to SQL.

Here’s where I push up my no-longer-existent glasses and say, “Well, actually, it’s a CASE expression rather than a statement because it always returns a value–or a dreaded NULL non-value–of some explicit data type.” But Chad is absolutely right about several T-SQL functions and operators being adaptations of the CASE expression syntax under the covers and I’m just being annoyingly pedantic for the fun of it.

One Comment

  1. Chad
    Chad 2024-08-05

    Yeah, I realized right after that it’s actually an “expression” even though I usually call it a “statement”. I considered fixing it after posting…but then I checked the SEO on it and realized almost _no one_ searches for “sql case expression”, so I left it as is and figured that post will just have to benefit from Cunningham’s law.

Comments are closed.