밥벌이/Database

[Oracle] null 값 순위변경

mypiece 2015. 9. 18. 22:11

Oracle에서는 정렬시 null값을 제일 마지막에 위치시킨다.(ms-sql은 반대) 
null 값을 제일 큰 값으로 인식한다는 말이다.

즉, 아래와 같다.


select *
from
(
 select 'aaa' col_1 from dual
 union all
 select 'bbb' from dual
 union all
 select null from dual
)
order by col_1;




​-----------------------------------------------------------------


하지만 order by에 옵션을 주어 이러한 null값에 순위를 바꿔줄 수가 있다. 


select *
from
(
 select 'aaa' col_1 from dual
 union all
 select 'bbb' from dual
 union all
 select null from dual
)
order by col_1 nulls first;


위와 같이 설정하게 되면 null 값이 제일 위에 위치하게 된다.
주의할 점은 해당 옵션은 null이 아닌 나머지 데이터 정렬에는 어떠한 영향도 주지 않는다.
단지 null값의 순위를 '처음' 또는 '끝'으로 바꿔준다.