在 sql server 2008 中将 varchar 转换为十进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1589707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:04:04  来源:igfitidea点击:

Converting varchar to decimal in sql server 2008

sqlsql-servertsqlsql-server-2008

提问by Developer

I have this data as varchar '00072330'. How do I convert it to a decimal that looks like '723.30' in SQL Server 2008?

我将此数据作为 varchar '00072330'。如何在 SQL Server 2008 中将其转换为类似于“723.30”的十进制?

回答by Rubens Farias

Try this:

尝试这个:

declare @data as varchar(8)
set @data = '00072330'
print cast(@data as decimal) / 100

回答by OMG Ponies

This:

这个:

SELECT CAST('00072330' AS INT)/100.0

...will give you:

...会给你:

723.300000

723.300000

The .0 is important, otherwise SQL Server will perform integer math.

.0 很重要,否则 SQL Server 将执行整数数学运算。