我有如下表。城市,州和郵政編碼都是varchar類型,可以是NULL。使用SQL CONCAT和合並時顯示的值是空
MyContacts
-id (int PK)
-city (varchar null)
-state (varchar null)
-zipcode (varchar null)
我想回到城市,州和郵編格式化爲一個單一的值,如下所示:
Seattle WA 98423 if all are not NULL
Seattle WA if zip is NULL
Seattle 98423 if state is NULL
WA 98423 if city is NULL
Seattle if state and zip is NULL
WA if city and zip is NULL
98423 if city and state is NULL
NULL if all of them are NULL
我一直在試圖像下面,但想到我會對此錯誤。由於有效
SELECT COALESCE(CONCAT(city," ",state," ",zipcode),CONCAT(city," ",states),zipcode) AS location
啊,看起來像我有倒退! – user1032531