Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

333
Views
create varchar column from boolean columns postgresql

I need to create a varchar column containing color value from boolean columns, here's the table structure :

CREATE TABLE public.prosp(
    id serial PRIMARY KEY NOT NULL,
    isblack bool,
    isyellow bool,
    isgreen bool
);

I want to add a column called color containg for example : green

if isgreen = true, I tried this and it worked

SELECT
"id",    
  CASE WHEN "isblack" THEN 'black; ' ELSE '' END ||
  CASE WHEN "isyellow" THEN 'yellow; ' ELSE '' END || 
  CASE WHEN "isgreen" THEN 'green; ' ELSE '' END
  AS couleurs
FROM public.prosp

now I need to put the result of the above expression in color column.

Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Is this what you want?

alter table public.prosp add column color varchar(255);

update public.prosp
    set color = (CASE WHEN isblack THEN 'black; ' ELSE '' END) ||
                (CASE WHEN isyellow THEN 'yellow; ' ELSE '' END) || 
                (CASE WHEN isgreen THEN 'green; ' ELSE '' END);

I should note that often this would be done using concat_ws():

update public.prosp
    set color = concat_ws(',',
                          (CASE WHEN isblack THEN 'black' END),
                          (CASE WHEN isyellow THEN 'yellow' END),
                          (CASE WHEN isgreen THEN 'green' END)
                         );

This puts a comma between the values, rather than a semicolon at the end.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!