2012-01-10 6 views
0
내가이 쿼리를 가지고 예를 들어

변경 값이 null의 경우

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

과 반환

내가 (SQL 문에) 모든 값을 대체 할
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 

(의 column5)가 null 인 경우 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 

이 가능하며, 어떻게 그렇다면? T-SQL (MS SQL 서버)에 대한 맞습니다

답변

3

사용 ISNULL

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

. 그는 어떤 DB 엔진이 사용되는지에 대해서는 언급하지 않았으므로, 대신에'COALESCE'를 사용할 것입니다. – bfavaretto

+1

COALESCE()뿐만 아니라 IFNULL()은 일부 서버에서도 사용됩니다. 다른 사람들에 대한 NVL(). 이는 서버 별 가변성이 많이있는 무언가입니다. COALESCE는이를 수행하는 SQL 표준 방법이므로 COALESCE 권장 사항은 좋지만 범용 적으로 사용할 수는 없습니다. –

관련 문제