序列號規則:T+時間+當天的序號,長度20位,時間跟序號之間的空用0補全
例如今天的第一條:T2015051200000000001,
第十條:T2015051200000000010,這種樣式的應該怎麼寫?
SQL SERVER
DECLARE @prefix varchar(9)
DECLARE @sn varchar(20)
DECLARE @daysn int
SET @prefix = 'T'+CONVERT(varchar(8),GetDate(),112)
SELECT @sn = MAX(sn)
FROM table1
WHERE sn LIKE @prefix+'%'
IF @sn IS NULL
SET @daysn = 1
ELSE
SET @daysn = CONVERT(int,SUBSTRING('T2015051200000000001',10,11))+1
SET @sn = @prefix+RIGHT('00000000000'+CONVERT(varchar(11),@daysn),11)
INSERT INTO table1 ...