Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to replace empty cells with "Blank" upon query in PostgreSQL

I am trying to deal with empty cells and get the data with empty cells filled with something like "Blank" string upon querying in PostgreSQL. My query looks like this :

SELECT t_id, a_date, c_date, o_s_date, o_e_date, 
   ttr_hr, ttn_min, d_sub_outage_impact,  
   tkt_source, d_vertical, d_grp, city, state
FROM r.all_t_event b

   Left Outer Join(
   select i_number,status,o_group
   From r.hpd_help_desk
   Group by i_number,status,o_group) a on a.i_number =b.t_id
Where close_date >= to_timestamp('10/05/2017','mm/dd/yyyy')
and t_condition = 'Outage'
and (a_grp like '%NOC%' or a.o_group like '%NOC%')
and t_id not in ('INC8788','INC26116')
and a.status = '5'

Tried a lot with the CASE statement in the SELECT statement, but I always get an error something like "ERROR: syntax error at or near "WHEN" when I tried with something like:

CASE 
WHEN d_outage_min = " " then "blank" else d_outage_min
WHEN v_outage_min = " " then "blank" else v_outage_min //Error occurred here
.....END

And got an error something like "ERROR: syntax error at or near "CASE" when I tried:

CASE 
WHEN d_outage_min = " " then "blank" else d_outage_min
END

CASE //Error occurred here

WHEN v_outage_min = " " then "blank" else v_outage_min
END
CASE...END

Will be glad to have a way out/Coalesce statement syntax/anything that could help. Thanks again!

like image 539
K Singh Avatar asked Oct 28 '25 05:10

K Singh


1 Answers

Rather than introducing the CASE statement, just surround your column (ie: col below) with the following. NULLIF will replace col with NULL if it finds an empty string. And COALESCE will replace the NULL with 'blank'. REPLACE function will not find an empty string, so this is the only way.

This will handle an empty string.

COALESCE(NULLIF(col,''), 'blank')

This will handle an empty string and a string with spaces.

COALESCE(NULLIF(TRIM(col),''), 'blank')

like image 57
MacGyver Avatar answered Oct 30 '25 17:10

MacGyver