My table has some null and empty strings, these look ugly when I query the table so I want to replace them with other values. I don't own the data so I can't modify the table itself, this needs to happen when I query.
I tried using regexp_replace to replace the empty strings with regexp_replace(column, '^$', '(no value found/some other custom message)' which didn't work
nulland empty string have to be treated separately. In addition, there's a difference between''(empty) and' '(blank).nullis "unknown".regexp_replace(null, '^$', 'default')doesn't do anything because the value it's matching is unknown. The match fails and it returnsnull.Instead, use
coalesce. It returns the first non-null value.coalesce(thing, 'default').To capture empty and blank strings, search for
^\s*$.regexp_replace(thing, '^\s*$', 'default').We put them together like so...
If
thingisnull,regexp_replacewill returnnull, andcoalescewill return'default'.If
thingis empty or blank,regexp_replacewill returndefaultandcoalescewill returndefault.If
thingis none of those,regexp_replacewill returnthingandcoalescewill returnthing.An alternative that might be easier to understand is:
If you like, you can put this into a function. We can declare it
immutable(the same arguments always produce the same result) potentially giving it a performance boost.Demonstration in PostgreSQL. Redshift is derived from PostgreSQL so it should work the same.