猿问

求一编号处理解决方案

我的数据库字段ID是自动增长的,但是客户要求把这个ID显示出来,并且按四位显示。

比如1,显示出来就是0001。我知道toString()好像是可以,但是不知道具体怎么实现。

请各路高手指点一二.....


人到中年有点甜
浏览 521回答 2
2回答

aluckdog

可以通过functino来解决代码use TestDb goCreate function GetTop4CharByPKID(@PKID int)returns nchar(4) as BeginDeclare @returnchar nchar(4)set @returnchar=(select case  when @PKID between 1 and 9 then '000'+cast(@PKID as nchar(4))when @PKID between 10 and 99 then '00'+cast(@PKID as nchar(4))when @PKID between 100 and 999 then '0'+cast(@PKID as nchar(4))when @PKID between 999 and 9999 then '0'+cast(@PKID as nchar(4))when @PKID >9999 then '....'end )return @returncharendgoselect dbo.GetTop4CharByPKID(1) as Numberunion allselect dbo.GetTop4CharByPKID(99) as Numberunion allselect dbo.GetTop4CharByPKID(100) as Numberunion allselect dbo.GetTop4CharByPKID(599) as Numberunion allselect dbo.GetTop4CharByPKID(50229) as Number 执行结果:Number0001009901000599....
随时随地看视频慕课网APP
我要回答