Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resolve this syntax error in a postgresql sql query to update multiple rows in a single column

I am needing to update multiple rows in a single column in a postgresql db

I have this:

UPDATE tableA
SET
  column_a = CASE WHEN column_a = 'conserve' THEN column_a = 'fixed',
  column_a = CASE WHEN column_a = 'balance' THEN column_a = 'moderate',
  column_a = CASE WHEN column_a = 'balance growth' THEN column_a = 'moderate growth',
  column_a = CASE WHEN column_a = 'aggressive' THEN column_a = 'moderate/agressive';

but am getting a syntax error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: syntax error at or near ","

>Solution :

You can do it with one single CASE statement:

UPDATE tableA
SET
  column_a = CASE WHEN column_a = 'conserve'       THEN 'fixed'
                  WHEN column_a = 'balance'        THEN 'moderate'
                  WHEN column_a = 'balance growth' THEN 'moderate growth'  
                  WHEN column_a = 'aggressive'     THEN 'moderate/agressive'
             END;

Try it here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading