append currency symbol to result of sql query

故事扮演 提交于 2019-12-04 03:50:08

问题


The result of a sql query

select PayerDate,PaymentAmount from Payments

PaymentAmount - decimal

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  $34.31
12/11/2012  $95.60
12/11/2012  $34.31

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


回答1:


you can concatenate it on your projection statement,

In MySQL,

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

In SQL Server,

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



回答2:


Try this Query

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



回答3:


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

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



回答4:


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.

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

Note: I only added parenthesis.




回答5:


The following can be tried:

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


来源:https://stackoverflow.com/questions/16433455/append-currency-symbol-to-result-of-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!