我希望MonthlyPayment有列float PaymentAmount
数字(18,3)
我在PaymentAmount中有这样的数据:
PaymentAmount
--------------
1230.25
829122.35
我想像这样返回付款金额:
PaymentAmount
--------------
000000001230.25
000000829122.35
我使用了这个查询,但它返回:
PaymentAmount
--------------
000000000001230
000000000829122
查询:
SELECT ISNULL(replace(str(MonthlyPayment.PaymentAmount,15,0),' ','0'),SPACE(15))
FROM MonthlyPayment
答案 0 :(得分:3)
对于SQL Server 2012 +,您可以使用FORMAT
SELECT FORMAT(PaymentAmount, '00000000000#.##')
FROM MonthlyPayment
示例执行:
DECLARE @PaymentAmount AS DECIMAL (18,2) = 1230.25;
SELECT FORMAT(@PaymentAmount,'00000000000#.##');
-- OUTPUT: 000000001230.25
DECLARE @PaymentAmount1 AS DECIMAL (18,2) = 829122.35;
SELECT FORMAT(@PaymentAmount1,'00000000000#.##');
-- OUTPUT: 000000829122.35
答案 1 :(得分:2)
如果您的SQL Server版本有REPLICATE
可用,那么您可以使用所需的零数小心地填充PaymentAmount
列,以使总字符串的宽度为15个字符。
SELECT REPLICATE('0',
15 - LEN(CONVERT(VARCHAR, CONVERT(DECIMAL(14, 2), PaymentAmount))))
+ CONVERT(VARCHAR, CONVERT(DECIMAL(14, 2), PaymentAmount))
FROM MonthlyPayment
答案 2 :(得分:1)
这适用于任何版本的SQL Server
SELECT RIGHT('000000000000' + CAST(CAST(PaymentAmount AS NUMERIC(18,2)) AS VARCHAR(20)),15)
FROM MonthlyPayment
Eample:
DECLARE @PaymentAmount AS DECIMAL (18,3) = 1230.256;
SELECT RIGHT('000000000000' + CAST(CAST(@PaymentAmount AS NUMERIC(18,2)) AS VARCHAR(20)),15)
DECLARE @PaymentAmount1 AS DECIMAL (18,3) = 829122.35;
SELECT RIGHT('000000000000' + CAST(CAST(@PaymentAmount1 AS NUMERIC(18,2)) AS VARCHAR(20)),15)