sqlpostgresqlnullpostgresql-10ifnull

Postgres convert empty string to NULL


I run a Postgres database and would like to convert empty string into NULL. The following snipped should do this with all columns, but it isn't working.

SELECT * FROM schema.table NULLIF(columnname,'');

The error message is:

ERROR:  syntax error at or near "''"
LINE 2:  NULLIF(columnname,'');

Solution

  • The following snipped converts empty strings into NULL:

    UPDATE schema.table SET columnname=NULL where columnname='';
    

    This works for me.