I have a table and the columns on this table contains empty spaces for some records. Now I need to move the data to another table and replace the empty spaces with a NULL
value.
I tried to use:
REPLACE(ltrim(rtrim(col1)),' ',NULL)
but it doesn't work. It will convert all of the values of col1
to NULL
. I just want to convert only those values that have empty spaces to NULL
.
I solved a similar problem using NULLIF
function:
UPDATE table
SET col1 = NULLIF(col1, '')
NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression.