(극미세팁)SQL Server int 변환시 overflow 발생

2019. 9. 17. 22:26

cast, convert 등의 함수를 쓸 때 분명히 overflow가 발생하지 않을것이라고 예상했는데 발생하는 경우가 있다.

 

아래의 예를 보자.

select convert(bigint,accounted_debit) from dbo.JE_Sample
where DR_CR = 'CR' and accounted_debit <> 0;

 

실행시키면 이런 메시지가 뜬다.

Msg 248, Level 16, State 1, Line 63
The conversion of the nvarchar value '2794014278' overflowed an int column.

 

분명 accounted_debit의 가장 큰 자리수는 14자리라 bigint로 충분히 가능하다,

하지만 왜 이런 현상이 발생할까...?

 

눈치채셨는지..?

 

올바른 쿼리는 다음과 같다.

 

select convert(bigint,accounted_debit) from dbo.JE_Sample
where DR_CR = 'CR' and convert(bigint,accounted_debit) <> 0;

 

where 절에서 convert함수(명시적 형변환)을 안해줬기 때문에 select 절이 아닌 where절에서 묵시적 형 변환으로 bigint 가 아닌 int로 변환하는 과정에서 overflow가 발생한 것이다!

 

이것을 깜빡하여 시간을 낭비하는 저 같은 사람이 없기를 바라며 극미세팁을 올린다.... ㅠㅠㅠ

'Oracle_DB_Unix admin' 카테고리의 다른 글

Oracle DBMS설치  (0) 2015.05.23

곰돌곰둘 Oracle_DB_Unix admin