SQL Server 2008 列的千位分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13857331/
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
SQL Server 2008 thousands separator for a column
提问by user1820705
I have a column called TotalArea
and its format is numeric (12,2)
.
我有一个名为的列TotalArea
,其格式为numeric (12,2)
.
I want it to display the numbers with a thousand separator so when I
我希望它显示带有千位分隔符的数字,所以当我
select TotalArea from table
to show me a format like 1,234.00
.
向我展示类似1,234.00
.
How could I do that? Thanks!
我怎么能那样做?谢谢!
回答by Robert
Try this way:
试试这个方法:
SELECT REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, TotalArea), 1), '.00', '')
FROM table
or
或者
SELECT CAST(CONVERT(VARCHAR, CAST(123456 AS MONEY), 1) AS VARCHAR)
FROM table
回答by dasiimwe
SELECT FORMAT(12345,'#,0.00');
SELECT FORMAT(TotalArea,'#,0.00') from table;
Reference: https://msdn.microsoft.com/en-us/library/ee634206(v=sql.105).aspx
参考:https: //msdn.microsoft.com/en-us/library/ee634206(v=sql.105).aspx
回答by Matt Whitfield
Formatting numbers for display is something that should be done in the display layer, and not within the database. So, in whatever application this data ends up being used, you should format it there. Management Studio, unfortunately, does not offer much control in this regard.
格式化显示数字应该在显示层中完成,而不是在数据库中。因此,无论最终在哪个应用程序中使用此数据,您都应该在那里对其进行格式化。不幸的是,Management Studio 在这方面没有提供太多控制。
回答by Adam-Ozbayraktar
I know the question is for sql server 2008 but if you have sql server 2012+, you can use format like so:
我知道问题是针对 sql server 2008 但如果你有 sql server 2012+,你可以使用这样的格式:
SELECT FORMAT(12345.5634, 2);
Reference: https://www.w3schools.com/sql/func_mysql_format.asp
回答by Samuel Darteh
Use this simple method:
使用这个简单的方法:
FORMAT(CHART_OF_ITEM.UNIT_PRIC_W_TAX, '#,0.00') AS UNIT_PRICE_W_TAX
回答by Airwings
Try this awesome example.
试试这个很棒的例子。
SELECT CAST(CONVERT(varchar, CAST(123456 AS money), 1) AS varchar)