开发者

Select Multiple columns into one field breaks if one or more columns are empty

开发者 https://www.devze.com 2023-02-24 18:18 出处:网络
Running PostgreSQL 7.4 (Yeah we are upgrading...) Query: SELECT ( \"Address\" ||\' \'|| \"Address 2\" ||\' \'||

Running PostgreSQL 7.4 (Yeah we are upgrading...)

Query:

SELECT (
     "Address" ||' '|| 
     "Address 2" ||' '|| 
     "City" ||' '|| 
     "State" ||' '|| 
     "Zip"
) AS full_address
FROM database
WHERE condition

All works as expected unless one or more of the fields are null/blank.

So if Address 2 is null/blank nothing is returned.

Question:

How can I use a IF (or CASE) condition to check if Address 2 has a value, if yes add it, el开发者_JAVA百科se skip


use coalesce:

select coalesce (someColumn, 'default-if-column-blank')


SELECT (
     "Address" ||' '|| 
     COALESCE("Address 2" ||' ', '') ||
     "City" ||' '|| 
     "State" ||' '|| 
     "Zip"
) AS full_address
FROM database
WHERE condition

http://www.postgresql.org/docs/7.4/static/functions-conditional.html#AEN11443

0

精彩评论

暂无评论...
验证码 换一张
取 消