0

I have a column in PostgreSQL Table

modification_dates timestamp without time zone[]

I need to be able to add a date to the array. The easy way would be to get the array and add element then rewrite it.

Another way would be to get the array , count it and use the following syntax to set the last value.

UPDATE my_table SET timestamp [countOfArray] = newDate;

Is there a way to do it with one SQL Statement?

2 Answers 2

3

You can use the concatenation operator or array_cat, as per the postgres documentation:

UPDATE my_table SET timestamp = timestamp || newDate;

or

UPDATE my_table SET timestamp = array_cat(timestamp, ARRAY[newDate]);

where the preferred way is the former. Adding a condition will work just as it does in any other update query.

Sign up to request clarification or add additional context in comments.

Comments

1

To append a new value to the array use the standard SQL concatenation operator:

update my_table
   set modification_dates = modification_dates||current_timestamp
where ...;

You can't insert a new element in between existing elements.

Comments

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.