Search code examples
sqlarraysgoogle-cloud-platformsql-updategoogle-cloud-spanner

how to update and append values to array in google spanner


I have a column with data type Array(String) in spanner. How can I append value to an array for update queries

I'm able to update the values using this command

update CarTable set models = ["BMW","HONDA"] WHERE year = "2020"

But the problem with this update is it overrides the previous values. I want to append these values to the ones that are already present. Is there any command that takes care of this requirement?


Solution

  • You can use the ARRAY_CONCAT function for that:

    update CarTable set models = ARRAY_CONCAT(models, ["BMW","HONDA"]) WHERE year = "2020"