4

sql查询的结果

select PayerDate,PaymentAmount from Payments

付款金额 - 小数

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

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

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

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

4

5 回答 5

7

您可以将其连接到您的投影语句中,

在 MySQL 中,

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

在 SQL Server 中,

SELECT PayerDate, '$' + CAST(PaymentAmount AS VARCHAR(15)) PaymentAmount
FROM   Payments
于 2013-05-08T05:45:00.757 回答
3

试试这个查询

select PayerDate,'$'+convert(varchar,PaymentAmount) as PaymentAmount
from Payments
于 2013-05-08T05:47:11.923 回答
1

您可以转换PaymentAmount为字符串,并在其前面加上美元:

select  PayerDate
,       '$' + cast(PaymentAmount as varchar(20)) as PaymentAmount
from    Payments
于 2013-05-08T05:44:28.317 回答
0

John Woo sql server 的答案是正确的,但您可能会收到类似“"$" is not a valid column name”的错误。为防止此错误,请通过添加括号进行一些外壳保存。

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

注意:我只添加了括号。

于 2019-11-12T18:26:19.627 回答
-1

可以尝试以下方法:

SELECT 
FORMAT([PayerDate], 'C' 'en-us') AS PayerDate ,
FORMAT([PaymentAmount], 'C', 'en-us') AS 'PaymentAmount' 
FROM Payments
于 2018-07-17T16:23:36.267 回答