执行SQL遇Msg 248错误:varchar值转换int列溢出
Hey there, let's break down this issue and fix it step by step. First, let's get to the root of why this error is popping up:
The value '5035899999' is way larger than the maximum limit of an INT column. In SQL Server (matching your error message format), an INT only supports values between -2,147,483,648 and 2,147,483,647. Your number (5,035,899,999) is more than double that upper bound, so trying to cram it into an INT causes an overflow.
Here are the most practical solutions:
Switch to a larger integer type
The simplest fix is to useBIGINTinstead ofINT. ABIGINTcan handle numbers up to 9,223,372,036,854,775,807—plenty of space for your value.
Example conversion:SELECT CAST('5035899999' AS BIGINT) -- If you need to update a table column: ALTER TABLE YourTableName ALTER COLUMN YourColumnName BIGINT;Use safe conversion functions to avoid crashes
If you can't modify the column type right now, useTRY_CASTorTRY_CONVERTto handle overflow gracefully. These functions returnNULLinstead of throwing an error when conversion fails, so you can filter out problematic values:-- Returns NULL for values too big for INT instead of breaking your query SELECT TRY_CAST(your_varchar_column AS INT) FROM YourTableName; -- Only keep values that actually fit in an INT SELECT your_varchar_column FROM YourTableName WHERE TRY_CAST(your_varchar_column AS INT) IS NOT NULL;Validate your data source (if needed)
If this value was supposed to fit in anINT, double-check your data origin—maybe there's a typo, or the data was intended to be a smaller number. But in most cases, upgrading toBIGINTis the right long-term fix.
内容的提问来源于stack exchange,提问作者ram

