Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatenating case statements 1

Status
Not open for further replies.
Jan 26, 2004
18
US
Hi All, thanks for reading my post. I am using a Postgres 7.3 and I have a query that contains lots of empty values, not null really, white space (the php code isnt mine)

Anyway, Im trying to format a report and I want to concatenate commas between my case statements like so

select id||', '||
base_lagtype_id||', '||
status_id||', '||
base_number||', '||
base_name||', '||
base_preds||', '||
base_duration||', '||
case
when base_start_date = '' then 'null'
else base_start_date
end as base_start_date||', '||
case
when base_end_date = '' then 'null'
else base_end_date
end as base_end_date
from sch_schedule_tasks
order by id

Unfortunately I get parse errors back. Anyone point me in the right direction for concatenating between case statements? Thanks
 
wow, it appears all I have to do is post and the answer will come to me.

when I end my case argument, I had
as <column_name)>||', '||

which causes the sql to parse error, remove that and add the cat string and blammo, works fine for what I needed. Thanks.
 
Code:
select id||', '||
base_lagtype_id||', '||
status_id||', '||
base_number||', '||
base_name||', '||
base_preds||', '||
base_duration||', '||
case
    when base_start_date = '' then 'null'
    else base_start_date
    end || ', '||
case
    when base_end_date = '' then 'null'
    else base_end_date
    end as base_end_date
from sch_schedule_tasks
order by id

That would work if all columns are character. If you have any non character data you must cast them before concatenating. E.g.

Code:
select cast(id as varchar(10)) || ',' ...

Btw, it is not a case statement, it's a case expression. A case statement in SQL is something else.
 
if the php code isn't yours, be careful -- i know you said the values were &quot;not null really&quot; but if any one of them is, the whole row of concatenated vallues will be

rudy
SQL Consulting
 
WORD, yeah Im running my SQL straight into the backend so Im not touching any of the php code but thanks for the heads up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top