2017-04-11 48 views
0

需要总结(金额),给它10个字符的 '0' 填充,更换空白SQL的使用填充

小数如:

Amount 
34.56 
45.12 
12.23 

答案应该像0000009191

+ cast((SELECT sum([amount]) from #clm2) as CHAR(10)) 

我该怎么做?

+4

哪个数据库引擎?你真的需要在数据库中而不是在表示层中进行吗? –

+0

[在SQL Server 2008中使用前导零填充字符串,因此它的长度为3个字符]的可能重复(http://stackoverflow.com/questions/16760900/pad-a-string-with-leading-zeros-so-its- 3-characters-long-in-sql-server-2008) – qxg

回答

0

你可以试试这个

用于填充

select '0000000000' + cast(fieldname as varchar) 

,并用填充替换空白点

select replace('0000000000' + cast(fieldname as varchar),'.','') 

得到10个字符从右

select right(replace('0000000000' + cast(fieldname as varchar),'.',''),10) 
1

如果2012 +考虑格式()

Declare @Yourtable table (amount decimal(10,2)) 
Insert Into @Yourtable values 
(34.56), 
(45.12), 
(12.23) 

Select Format(sum(Amount*100),'0000000000') 
From @YourTable 

返回

0000009191 
+0

你对“2012+”有什么意思?没有这样的SQL标准 –