Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda