Boolean value based on length of other field of row postgres

Viewed 19

I have crated table which stores value and concats another based on event in applictaion Table look I want to set completed value as TRUE once date length of chars exceeds 20, is it possible to do in postgres? Thanks in advance

I am not sure what to do, I used more detailed functionalities of db more than 3 years ago last time.

1 Answers

You can define the column completed as a generated column and set the generation code based on the length of the date column (seems a particularly poor column name). So:

create table event_list(id         integer generated always as identity
                                           primary key
                       , date      character varying(100)
                       , completed boolean generated always as (length(date) > 20) stored
                       ); 

See demo.
The above works in v12 and above. For prior versions you create a trigger on insert and update that sets the value for completed column. (included in demo)

Related