将货币符号附加到 sql 查询的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16433455/
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 15:07:26  来源:igfitidea点击:

append currency symbol to result of sql query

sqlsql-serversql-server-2008-r2

提问by user1282609

The result of a sql query

一个sql查询的结果

select PayerDate,PaymentAmount from Payments

PaymentAmount - decimal

PaymentAmount - 十进制

Date        Amount
12/11/2012  34.31
12/11/2012  95.60
12/11/2012  34.31

is that possible to get the result of query as below:

是否可以得到如下查询结果:

Date        Amount
12/11/2012  .31
12/11/2012  .60
12/11/2012  .31

I have tried but couldn't find much info on this.

我已经尝试过,但找不到关于此的太多信息。

回答by John Woo

you can concatenate it on your projection statement,

您可以将其连接到您的投影声明中,

In MySQL,

SELECT PayerDate, CONCAT('$', PaymentAmount) PaymentAmount
FROM Payments

在 MySQL 中,

SELECT PayerDate, CONCAT('$', PaymentAmount) PaymentAmount
FROM Payments

In SQL Server,

在 SQL Server 中,

SELECT PayerDate, '$' + CAST(PaymentAmount AS VARCHAR(15)) PaymentAmount
FROM   Payments

回答by Luv

Try this Query

试试这个查询

select PayerDate,'$'+convert(varchar,PaymentAmount) as PaymentAmount
from Payments

回答by Andomar

You could convert PaymentAmountto a string, and prefix it with a dollar:

您可以转换PaymentAmount为字符串,并以美元作为前缀:

select  PayerDate
,       '$' + cast(PaymentAmount as varchar(20)) as PaymentAmount
from    Payments

回答by escha

John Woo sql server answer is right but you might get an error like " "$" is not a valid column name". To prevent this error do some housing keeping by adding parenthesis.

John Woo sql server 的答案是正确的,但您可能会收到类似“"$" 不是有效的列名" 之类的错误。为防止出现此错误,请通过添加括号来保留一些住房。

SELECT PayerDate, ('$' + CAST(PaymentAmount AS VARCHAR(15))) AS PaymentAmount
FROM   Payments

Note: I only added parenthesis.

注意:我只添加了括号。

回答by sandeep

The following can be tried:

可以尝试以下方法:

SELECT 
FORMAT([PayerDate], 'C' 'en-us') AS PayerDate ,
FORMAT([PaymentAmount], 'C', 'en-us') AS 'PaymentAmount' 
FROM Payments