一个表内的一列数据如下:
2009092000
2009092001
2009092002
2009092003
2009092004
2009092100
2009092101
2009092102
2009092103
2009092104
2009092200
2009092201
2009092202
2009092203
2009092204现条件是 查询 20090920 到 20090922 之间每天00和02的数据,想要的数据如下:
(其实并不是只有00到02数据,应该是00到71,只是为了简单说明)
2009092000
2009092001
2009092002
2009092100
2009092101
2009092102
2009092200
2009092201
2009092202十分感谢!

解决方案 »

  1.   

    select * 
    from tb
    where 
    left(col,8) between '20090920' and '20090922'
    and
    right(col,1) between '00' and '02'
      

  2.   

    select * 
    from tb
    where 
    left(col,8) between '20090920' and '20090922'
    and
    right(col,2) between '00' and '02'
    1楼2写成1了
      

  3.   


    --> 测试数据:[TA]
    if object_id('[TA]') is not null drop table [TA]
    create table [TA]([A] int)
    insert [TA]
    select 2009092000 union all
    select 2009092001 union all
    select 2009092002 union all
    select 2009092003 union all
    select 2009092004 union all
    select 2009092100 union all
    select 2009092101 union all
    select 2009092102 union all
    select 2009092103 union all
    select 2009092104 union all
    select 2009092200 union all
    select 2009092201 union all
    select 2009092202 union all
    select 2009092203 union all
    select 2009092204select * from [TA] where cast(right(A,2) as int) between 0 and 2/*
    A           
    ----------- 
    2009092000
    2009092001
    2009092002
    2009092100
    2009092101
    2009092102
    2009092200
    2009092201
    2009092202(所影响的行数为 9 行)*/
    drop table TA
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col] int)
    insert [tb]
    select 2009092000 union all
    select 2009092001 union all
    select 2009092002 union all
    select 2009092003 union all
    select 2009092004 union all
    select 2009092100 union all
    select 2009092101 union all
    select 2009092102 union all
    select 2009092103 union all
    select 2009092104 union all
    select 2009092200 union all
    select 2009092201 union all
    select 2009092202 union all
    select 2009092203 union all
    select 2009092204select * 
    from tb
    where 
    left(col,8) between '20090920' and '20090922'
    and
    right(col,2) between '00' and '02'
    --测试结果:
    /*
    col
    -----------
    2009092000
    2009092001
    2009092002
    2009092100
    2009092101
    2009092102
    2009092200
    2009092201
    2009092202(9 行受影响)
    */
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-18 00:33:36
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
    -- May  3 2005 23:18:38 
    -- Copyright (c) 1988-2003 Microsoft Corporation
    -- Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] int)
    insert [tb]
    select 2009092000 union all
    select 2009092001 union all
    select 2009092002 union all
    select 2009092003 union all
    select 2009092004 union all
    select 2009092100 union all
    select 2009092101 union all
    select 2009092102 union all
    select 2009092103 union all
    select 2009092104 union all
    select 2009092200 union all
    select 2009092201 union all
    select 2009092202 union all
    select 2009092203 union all
    select 2009092204
    --------------开始查询--------------------------select * from [tb] where right(col,2) in('00','01','02') and left(col,len(col)-2) between 20090920 and 20090922
    ----------------结果----------------------------
    /* col         
    ----------- 
    2009092000
    2009092001
    2009092002
    2009092100
    2009092101
    2009092102
    2009092200
    2009092201
    2009092202(所影响的行数为 9 行)
    */
      

  6.   

    create table [TA]([A] int)
    insert [TA]
    select 2009092000 union all
    select 2009092001 union all
    select 2009092002 union all
    select 2009092003 union all
    select 2009092004 union all
    select 2009092100 union all
    select 2009092101 union all
    select 2009092102 union all
    select 2009092103 union all
    select 2009092104 union all
    select 2009092200 union all
    select 2009092201 union all
    select 2009092202 union all
    select 2009092203 union all
    select 2009092204select * from ta where left(a,8) between '20090920' and '20090922' and right(a,2) between '00' and '02' drop table ta/*
    A           
    ----------- 
    2009092000
    2009092001
    2009092002
    2009092100
    2009092101
    2009092102
    2009092200
    2009092201
    2009092202(所影响的行数为 9 行)*/
      

  7.   

    select * from tb
    where left(col,8) between '20090920' and '20090922'
    and right(col,2) in ('00' ,'01', '02')