feetwet feetwet - 11 months ago 42
SQL Question

Why does MS SQL CAST as bigint result in int overflow?

Why does the following T-SQL statement:

select CAST(COALESCE('3537601039',0) as bigint)

result in the following error when run against MS SQL Server 10?

The conversion of the varchar value '3537601039' overflowed an int

Is there a "more correct" way to do this conversion of a
to a
that would avoid this problem when the number is within range of a


The first thing that is happening is that your string is trying to convert to a regular int to match the 0. Try this:

select CAST(COALESCE('3537601039','0') as bigint)