2012-01-10 58 views
0

例如,我有這個疑問變化值,如果它是空

select column1, column2, column3, column4, column5 from mytable 

,並返回

example1|null |example3|example4|example5 
examplea|exampleb|examplec|exampled|null 
examplex|exampley|null |examplexx|exampleyy 
exampleh|null |examplek|examplel|examplem 
example1|example2|example3|example4|null 

我想要替換(SQL語句)的所有值(第5列)如果它們爲空,則用0示例替換它們。所以,我想結果轉換上面這個

example1|null |example3|example4|example5 
examplea|exampleb|examplec|exampled|0 
examplex|exampley|null |examplexx|exampleyy 
exampleh|null |examplek|examplel|examplem 
example1|example2|example3|example4|0 

是否有可能,如果又如何?

回答

3

使用ISNULL

select column1, column2, column3, column4, ISNULL(column5,0) as column5 
from mytable 
+3

這是正確的T-SQL(MS SQL服務器)。由於他沒有說明哪個數據庫引擎正在被使用,我會用'COALESCE'來代替。 – bfavaretto 2012-01-10 21:22:38

+1

除了COALESCE(),IFNULL()也用於某些服務器上; NVL()等。這是有很多服務器特定的變化。 COALESCE是SQL標準的做法,所以COALESCE建議是好的,但不是普遍可用的。 – 2012-01-10 21:25:22