开发者

In SQL Server 2000 how do you add 0's to beginning of number to fill nchar(n)

I'm trying to add 0's to the beginning of an nchar(n). Is their a format function in SQL Server 2000 to开发者_如何学Go add 0's to an int so that it will always have n number of digits with leading 0's:

example

int         nchar(n)
1           0000..1
2           0000002
3           0000003
...
10          0000010
11          0000011
...
100         0000100
...
1000        0001000


Sorry for being slightly off-topic, but are you really sure that this is a problem that should be dealt with on a database level rather than in presentation level? I mean, database can and should store those numbers as-is, and only in presentation code do you add all the leading zeroes.


e.g. for n=12

DECLARE @foo bigint
DECLARE @bar bigint

SET @foo=12345678901
SET @bar=12

SELECT RIGHT('000000000000' + CAST(@foo AS VARCHAR(12)),12) 
SELECT RIGHT('000000000000' + CAST(@bar AS VARCHAR(12)),12) 

Beware! This won't work for numbers with more than n digits!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜