对4的处理

来源:互联网 发布:linux制作快捷方式 编辑:程序博客网 时间:2024/05/01 20:24

因为4在某些人看来是不吉祥的数字,
所以在我最近的一个系统里面的数字
根据客户的要求得不出现4。

下面是我要说的问题的SQL表示:
declare @i int
select @i = abc from table
如果该@i数字里面有一个数字是4,则加1,直到无4为止。
比如:
@i = 1,则@i =  2
@i = 4,则@i =  5
@i = 34,则@i =  35
@i = 343,则@i = 350

以下是相关的解决办法:
declare @i int
set @i=3434567
select left(@i, case charindex('4',@i)-1
                     when -1 then len(@i)
                     else charindex('4',@i)-1
                end)+
       case charindex('4',@i)-1
            when -1 then ''
            else '5'
       end+
       REPLICATE('0',len(@i)-case charindex('4',@i)-1
                                   when -1 then len(@i)
                                   else charindex('4',@i)  
                             end)