流水号的问题。在线等。
T1表如下:
ID wh
1 0705010001
2 0705010002
3 0705010003
其中wh为char(10)
wh前六位为年月日后四位为流水号
现在我要新增记录?(当天最大的流水怎样得到)
------解决方案--------------------max(RIGHT( '0705010003 ',4))
------解决方案--------------------select max(wh) from t1
------解决方案--------------------select max(RIGHT(wh,4)) from T1 ??
------解决方案--------------------insert into T1(wh)
select right( '000 '+rtrim(cast(isnull(right(max(wh),4),0) as int)+1),4)
from T1
where left(wh,6)=convert(char(6),cast( '2007-05-02 ' as datetime),12)
------解决方案--------------------create table T1(wh char(10))
insert into T1
select '0705010001 '
union all select '0705010002 '
union all select '0705010003 '
insert into t1
select isnull(left(max(wh),6)+right( '0000 '+cast(cast(right(max(wh),4) as int)+1 as varchar(4)),4),right(replace(convert(char(10),getdate(),21), '- ', ' '),6)+ '0001 ')
from t1
where left(wh,6)=right(replace(convert(char(10),getdate(),21), '- ', ' '),6)
select * from t1
drop table t1