22

I have to do some queries on a messy database. Some columns are filled with either null or an empty string. I can do a query like this:

select * from a where b is not null and b <> '';

But is there a shortcut for this case? (match every "not empty" value) Something like:

select * from a where b is filled;
rap-2-h
  • 30,204
  • 37
  • 167
  • 263

2 Answers2

53

Just:

where b <> ''

will do what you want as null <> '' is null and the row will not be returned

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
  • 1
    or `LTRIM(b) <> ''` if there's the possibility of rows with more than a single space. – Nick Jul 07 '22 at 15:54
  • `invalid input syntax for type integer: ""` so, if column b is INT then `b <> 0` but your answer is enough correct for the Q: '...or an empty string' – Vasilii Suricov Jul 26 '23 at 13:01
3

select * from a where COALESCE(b, '') <> '';

Steve Smith
  • 2,244
  • 2
  • 18
  • 22