0

Append text to column data in PostgreSQL if columns initial value is null. Its not altering value.

0

1 Answer 1

14

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 ...
Sign up to request clarification or add additional context in comments.

3 Comments

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.
@SamiKuhmonen: you are right. I just wanted to make clear that appending to a null value will not work as intended.
Use concate(the_column, 'xyz') for appending to null column. set the column= the_column || 'xyz' does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.