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

Postgresql generator column is causing a syntax error

I’m adding a generator column to a Postgresql v14.8 table using pgAdmin4 with this expression:

CASE WHEN columnA > 0 THEN columnA / columnB

But am getting this syntax error:

syntax error at or near ")" LINE 16: …> 0 THEN columnA / columnB)
STORED;^

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

I’ve looked at multiple examples and can’t identify what I’ve done wrong.

EDIT

Full SQL code as requested:

ALTER TABLE IF EXISTS public."table_name_here"
    ADD COLUMN columnA integer GENERATED ALWAYS AS (
       CASE WHEN columnA > 0 THEN columnA / columnB
) STORED;

>Solution :

Use the following code to create a generated column in PostgreSQL 15.4; simply replace "your_table" with the real name of your table.

ALTER TABLE your_table
ADD COLUMN generated_column numeric GENERATED ALWAYS AS (
    CASE WHEN columnA > 0 THEN columnA / columnB ELSE NULL END
);
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