开发者

SQL Server 2008 - conversion from string to int fails

开发者 https://www.devze.com 2023-03-31 08:04 出处:网络
Don\'t know why this conversion fails. SELECT CAST(\'32.999\' AS INT) throws the error saying Conversion failed when converting the varchar value \'32.000\' to data

Don't know why this conversion fails.

SELECT CAST('32.999' AS INT)

throws the error saying

Conversion failed when converting the varchar value '32.000' to data type int.

开发者_如何学Python

I know I can convert it to Decimal and then convert it to Int but why 32.000 is not being converted to Int directly?

Am I missing something here?

--EDIT--

I don't care about the decimal points. I want SQL to ignore them.


32.000 can not be represented accurately as an int due to the decimal places. Instead, if you want a numeric value with decimals, you can use a float (or numeric) data type.

Select CAST('32.000' as float)

edit: misread your question a little bit.

I'm assuming SQL is trying it's best to preserve your intentions. When it reads in that the numeric value you're attempting to cast has decimal values, it will flat out reject it.


used SELECT CAST(ROUND('32.999',0,1) AS INT) and it works well.

0

精彩评论

暂无评论...
验证码 换一张
取 消