一个SQL的语法求解释 select '00'+REPLICATE('0',4-len(cast(cast(isnull(max(right(f_code3,4)),'000') as int)+1 as varchar(4))))+cast(cast(isnull(max(right(f_code3,4)),'0000') as int)+1 as varchar(4)) from table
操作如下:
REPLICATE('0',4-len(cast(cast(isnull(max(right(f_code3,4)),'000') as int)+1 as varchar(4))?
--取最大值(返回字符串中从右边开始指定后4个字符)
1
|
max ( right (f_code3,4)) |
-- isnull函数 如果为空返回'000'三个零的字符串
1
|
isnull ( max ( right (f_code3,4)), '000' ) |
--cast 转换为int 类型 加上1 转换成varchar(4)长度为4的字符串
1
|
cast ( cast ( isnull ( max ( right (f_code3,4)), '000' ) as int )+1 as varchar (4)) |
-- 4减去len(字符串) 长度的值
1
|
4-len( cast ( cast ( isnull ( max ( right (f_code3,4)), '000' ) as int )+1 as varchar (4)) |
--REPLICATE 指定有多少个'0'字段串
1
|
REPLICATE( '0' ,4-len( cast ( cast ( isnull ( max ( right (f_code3,4)), '000' ) as int )+1 as varchar (4)))) |
如有问题可以追问,我当及时回答.
希望能帮到你!
追问
解释的很详细,只是不懂这串语法要实现什么
追答
如果没猜错,每次查询编号取最大值.前面补0的操作.
如:查询结果:
第一个 第二个当前值
0004567 4567