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

耗尽温柔 提交于 2019-12-08 13:41:35

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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!