Append text to column data in PostgreSQL if columns initial value is null. Its not altering value.
1 Answer
It's pretty unclear what you are trying to achieve, but:
If the column's value is null you can't "append" a value to it because any expression involving null yields null ( null||'foo' is null). In this case you just replace the null value with the new value:
update the_table
set the_column = 'new value'
where the_column is null;
If with "initial value is null" you mean if the "current value is an empty string", then you would do something like this:
update the_table
set the_column = the_column || 'this will be appended'
where the_column = '';
Which is identical to:
update the_table
set the_column = 'this will be appended'
where the_column = '';
null and '' are different things
Another option is to use the concat() function which will implicitly treat null values as empty strings:
update the_table
set the_column = concat(the_column, 'this will be appended')
where ...
3 Comments
Sami Kuhmonen
Then again, if it is a null or empty string, the end result is the same since '' || 'appended' will be just 'appended'. But depends on OPs actual needs.
Paweł Miłosz
Use concate(the_column, 'xyz') for appending to null column. set the column= the_column || 'xyz' does not work.