表中有一列varchar(32)的,准备用这一列来做编号
这一列现有是:
000000000
000000002
怎样获得他们中间的 000000001
有没有简练的sql语言,我不想用循环去找呀
大神们帮帮忙
------解决方案--------------------
select number from master..spt_values where type='P' and number>='1' and number<=(select max(convert(int,id)) from T)
and not exists (select * from T where convert(int,id)=number)
------解决方案--------------------
WITH a1 (id)AS
(
SELECT '000000000' UNION ALL
SELECT '000000002' UNION ALL
SELECT '000000003' UNION ALL
SELECT '000000005'
)
,a2 AS
(
SELECT CAST(MAX(id) AS INT) id FROM a1
)
,a3 AS
(
SELECT 0 n,CAST('000000000' AS CHAR(9)) id
UNION ALL
SELECT n+1,CAST(RIGHT('00000000'+CAST(n+1 AS VARCHAR(9)),9) AS CHAR(9))
FROM a3
WHERE n<(SELECT id FROM a2)
)
SELECT id FROM a3
EXCEPT
SELECT id FROM a1
OPTION(MAXRECURSION 0)