在 SQL Server 2008 R2 中用逗号格式化数字但没有小数? [英] Format a number with commas but without decimals in SQL Server 2008 R2?

查看:40
本文介绍了在 SQL Server 2008 R2 中用逗号格式化数字但没有小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容在 T-SQL 中创建逗号格式的数字.我怎样才能摆脱小数点和小数点后的数字.所以如果我格式化后得到 1,112.00,我怎么会只得到 1,112?

I am using the following to create a comma formatted number in T-SQL. How can I get rid of the decimal point and the digits after decimal. So if I get 1,112.00 after formatting, how would I get only 1,112?

SELECT CONVERT(varchar, CAST(1112 AS money), 1)

推荐答案

DECLARE @val varchar(50)

set @val = CONVERT(varchar(50), CAST(1112 AS money), 1)
SELECT  left(@val, len(@val) - 3)

这也适用于小数点后的数字:

This also works with digits after the decimal point:

DECLARE @val varchar(50)

set @val = CONVERT(varchar(50), CAST(1112.56 AS money), 1)
SELECT  left(@val, len(@val) - 3)

注意:正如@Mahmoud Gamal 指出的那样,格式化通常更适合在前端执行.

Note: as @Mahmoud Gamal points out, formatting is often more suited to be performed in the front-end.

这篇关于在 SQL Server 2008 R2 中用逗号格式化数字但没有小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆