2017-04-11 50 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標準 –