问题
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