现在有一张表,大致如下:开始日     结束日      名字
19990708   20000629    名字1
20000630   20010709    名字2
20010710   20030707    名字3
20030708   20040701    名字4
20040702   20050630    名字5
20050701   20060630    名字6
20060701   20070131    名字7
20070201   20110701    名字8
20110703   20111001    名字9现在我想抽出开始日是20060401,结束日是20060731 之间的所有名字。
也就是名字7和名字8.
sql该如何写呢

解决方案 »

  1.   

    select * from tb where 开始日>='20060401' and 结束日<='20060731'
      

  2.   

    select * from 表 where convert(varchar(32),开始日期,112)>='20060401' and convert(varchar(32),结束日期,112)<='20060731'
      

  3.   


    declare @T table (开始日 datetime,结束日 datetime,名字 varchar(5))
    insert into @T
    select '19990708','20000629','名字1' union all
    select '20000630','20010709','名字2' union all
    select '20010710','20030707','名字3' union all
    select '20030708','20040701','名字4' union all
    select '20040702','20050630','名字5' union all
    select '20050701','20060630','名字6' union all
    select '20060701','20070131','名字7' union all
    select '20070201','20110701','名字8' union all
    select '20110703','20111001','名字9'select * from @T where 开始日>'2006-04-01' and 结束日<'2011-10-01' 
    /*
    开始日                     结束日                     名字
    ----------------------- ----------------------- -----
    2006-07-01 00:00:00.000 2007-01-31 00:00:00.000 名字7
    2007-02-01 00:00:00.000 2011-07-01 00:00:00.000 名字8
    */
      

  4.   


    --你结束日期设2011年的还差不多吧?设2006年的根本没结果的,如下--> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (开始日 varchar(10),结束日 varchar(10),名字 varchar(5))
    insert into [tb]
    select '19990708','20000629','名字1' union all
    select '20000630','20010709','名字2' union all
    select '20010710','20030707','名字3' union all
    select '20030708','20040701','名字4' union all
    select '20040702','20050630','名字5' union all
    select '20050701','20060630','名字6' union all
    select '20060701','20070131','名字7' union all
    select '20070201','20110701','名字8' union all
    select '20110703','20111001','名字9'--开始查询
    select * from [tb] where 开始日>='20060401' and 结束日<='20110731'--结束查询
    drop table [tb]/*
    开始日        结束日        名字
    ---------- ---------- -----
    20060701   20070131   名字7
    20070201   20110701   名字8(2 行受影响)
      

  5.   

    晕,写错了
    开始日 结束日 名字
    19990708 20000629 名字1
    20000630 20010709 名字2
    20010710 20030707 名字3
    20030708 20040701 名字4
    20040702 20050630 名字5
    20050701 20060630 名字6
    20060701 20070131 名字7
    20070201 20110701 名字8
    20110703 20111001 名字9现在我想抽出开始日是20060401,结束日是20110702 之间的所有名字。
    也就是名字7和名字8.select * from tb where 开始日>='20060401' and 结束日<='20110702'
    取不出来sql该如何写呢
      

  6.   

    create table tb(开始日 datetime,结束日 datetime,名字 nvarchar(10))
    insert into tb select '19990708','20000629','名字1'
    insert into tb select '20000630','20010709','名字2'
    insert into tb select '20010710','20030707','名字3'
    insert into tb select '20030708','20040701','名字4'
    insert into tb select '20040702','20050630','名字5'
    insert into tb select '20050701','20060630','名字6'
    insert into tb select '20060701','20070131','名字7'
    insert into tb select '20070201','20110701','名字8'
    insert into tb select '20110703','20111001','名字9'
    go
    select * from tb where 开始日>='20060401' and 结束日<='20060731' 
    --如果是楼主说的"在"某某日的话,上面的比较运算符应改为"="
    /*
    开始日                     结束日                     名字
    ----------------------- ----------------------- ----------(0 行受影响)*/
    go
    drop table tb
      

  7.   

    create table tb(开始日 datetime,结束日 datetime,名字 nvarchar(10))
    insert into tb select '19990708','20000629','名字1'
    insert into tb select '20000630','20010709','名字2'
    insert into tb select '20010710','20030707','名字3'
    insert into tb select '20030708','20040701','名字4'
    insert into tb select '20040702','20050630','名字5'
    insert into tb select '20050701','20060630','名字6'
    insert into tb select '20060701','20070131','名字7'
    insert into tb select '20070201','20110701','名字8'
    insert into tb select '20110703','20111001','名字9'
    go
    select * from tb where 开始日>='20060401' and 结束日<='20110702' 
    /*
    开始日                     结束日                     名字
    ----------------------- ----------------------- ----------
    2006-07-01 00:00:00.000 2007-01-31 00:00:00.000 名字7
    2007-02-01 00:00:00.000 2011-07-01 00:00:00.000 名字8(2 行受影响)
    */
    go
    drop table tb