Is there something similar to the coalesce function for working with a CHAR column that may be blank?
For example, let's say I want to use PREFERRED_NAME
if it exists, but use FIRST_NAME
if it doesn't and the table I'm working with has data like this?
+-------------+----------------+
| FIRST_NAME | PREFERRED_NAME |
+-------------+----------------+
| JOHN | |
| STEPHANIE | |
| STEPHEN | STEVE |
+-------------+----------------+
I was hoping to be able to write something like this:
SELECT COALESCE(PREFERRED_NAME,FIRST_NAME)
FROM COMPANY.EMPLOYEES
... but it looks like COALESCE
only works for NULL
since for the data above my results look like this:
+---------+
| Column1 |
+---------+
| |
| |
| STEVE |
+---------+
I found a potential solution in a question for MySQL that looks like this:
SELECT COALESCE(NULLIF(PREFERRED_NAME,''),FIRST_NAME)
Is using NULLIF
or writing a CASE
statement the best solution at this point? I was hoping for a single function that'd do it for me.