27,579
社区成员
发帖
与我相关
我的任务
分享
select
number
from
(select substring(col,7,3) as col from [tb]) p
right join
(select number from master..spt_values where type='p' and number between 101 and 110) k
on
p.COL=k.number
where
COL is null
CREATE TABLE t1(a VARCHAR(1000),fl VARCHAR(1000),qs VARCHAR(1000))
INSERT t1 SELECT 'fcc090101a,fcc090102a,fcc090103a,fcc090106a,fcc090107a,fcc090110a' ,NULL,null
UPDATE t1 SET fl=REPLACE(REPLACE(a+',','fcc090',''),'a,',',')
DECLARE @sql VARCHAR(1000)
DECLARE @i INT ,@min VARCHAR(4),@max VARCHAR(4)
SELECT @min=LEFT(fl,3),@max=left(RIGHT(fl,4),3), @i=@max*1-@min*1+1 FROM t1
IF OBJECT_ID('t2') IS NOT NULL
DROP TABLE t2
EXEC('create table t2(id int identity('+@min+',1),b int)')
SET ROWCOUNT @i
INSERT t2 SELECT 0 FROM syscolumns s
SET ROWCOUNT 0
SELECT @sql=ISNULL(@sql+',','')+LTRIM(id) FROM t2 ,t1 WHERE CHARINDEX(LTRIM(ID),a)=0
UPDATE t1 SET qs=@sql
SELECT * FROM t1
/*
Micsosoft Windows 7.0 7600
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
小弟愿和大家共同进步、共同学习!
如有雷同、实属巧合
●●●●●2009-09-29 14:06:47.163●●●●●
★★★★★soft_wsx★★★★★
*/
if object_id('ta')is not null drop table ta
go
CREATE TABLE ta(id int identity(1,1) primary key,name nvarchar(20))
INSERT ta(name)
select 'fcc090101a'
union all select 'fcc090102a'
union all select 'fcc090103a'
union all select 'fcc090106a'
union all select 'fcc090107a'
union all select 'fcc090110a'
select SUBSTRING(name,7,3) as name into #a from ta
select a.name from #a a left join #a b on a.name=b.name
select a.name
from(select 101+number as name
from master..spt_values
where type='P' and number<10
)a where not exists(select 1 from #a where name=a.name)
/*
name
104
105
108
109
*/
if object_ID('ta') IS NOT NULL DROP TABLE ta
go
create table ta(col varchar(20) )
go
insert ta select
'fcc090101a' union all select
'fcc090102a' union all select
'fcc090103a' union all select
'fcc090106a' union all select
'fcc090107a' union all select
'fcc090110a'
select cast(substring(col,7,3)as int) as num into #t from ta
select top (select max(num) from #t) id=identity(int,1,1) into #s from sysobjects a ,sysobjects b
select id from #s where id not in(select num from #t)
and id>(select min(num) from #t)
(110 行受影响)
id
-----------
104
105
108
109
(4 行受影响)
drop table #t
drop table #s
Select A.Number
from
(Select number from master..spt_values where type='P' and number between 101 and 110)
A left join
(select distinct convert(int,SUBSTRING(ItemCode,7,3)) as substr from OITM where ItemCode between 'fcc090101a' and 'fcc090110a' ) B on A.number = B.substr
where B.substr is null
/*
Number
-----------
104
105
108
109
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-29 14:54:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] varchar(10))
insert [tb]
select 'fcc090101a' union all
select 'fcc090102a' union all
select 'fcc090103a' union all
select 'fcc090106a' union all
select 'fcc090107a' union all
select 'fcc090110a'
--------------开始查询--------------------------
;with f as
(
select substring(col,7,3) as col from [tb]
)
select
number
from
f p
right join
(select number from master..spt_values where type='p' and number between 101 and 110) k
on
p.COL=k.number
where
COL is null
----------------结果----------------------------
/* number
-----------
104
105
108
109
(4 行受影响)
*/
先截取数据出来
然后
select number
from tb p right join (select number from master..spt_values where type='p' and number between 101 and 110) k
on p.COL1=k.number
where COL1 is null