表里日期是分开3个字段年,月,日记录的。请问我要查询2008年2月3日到2009年12月14日的数据sql语句怎么写啊?谢谢

解决方案 »

  1.   

    select *
    from tb
    where cast(ltrim(年)+'-'+ltrim(月)+'-'+ltrim(日)as datetime)
    between '2008-02-03' and '2009-12-14'
      

  2.   


    --如果年月日字段为字符型
    select * 
    from tb
    where cast(年+'-'+月+'-'+日 as datetime)>='2008-2-3'
        and cast(年+'-'+月+'-'+日 as datetime)<='2009-12-14'
      

  3.   

    DECLARE @T TABLE
        (
          id INT IDENTITY(1, 1) ,
          [Y] VARCHAR(10) ,
          [M] VARCHAR(10) ,
          [D] VARCHAR(10)
        )INSERT INTO @T([Y],[M],[D])
    SELECT '2008','9','10' UNION ALL
    SELECT '2008','2','1' UNION ALL
    SELECT '2010','1','1' UNION ALL
    SELECT '2009','10','8' SELECT * FROM @t WHERE [y]+'年'+[M]+'月'+[D]+'日' BETWEEN '2008年2月3日' AND '2009年12月14日'/*(4 行受影响)
    id          Y          M          D
    ----------- ---------- ---------- ----------
    1           2008       9          10
    4           2009       10         8(2 行受影响)
    */
      

  4.   

    但有个问题是我的数据库是access,不能用cast。年、月、日的数据类型是数字,很烦啊,不知道怎么写sql语句
      

  5.   

    SELECT CDate(CStr(Year)+'-'+CStr(Month)+'-'+CStr(Day)) 
    FROM test
    where CDate(CStr(Year)+'-'+CStr(Month)+'-'+CStr(Day))  between CDate('2008-2-3') and CDate('2009-12-14')
      

  6.   

    SELECT CDate(CStr(Year)+'-'+CStr(Month)+'-'+CStr(Day))  
    FROM test
    where CDate(CStr(Year)+'-'+CStr(Month)+'-'+CStr(Day)) between CDate('2008年2月3日') and CDate('2009年12月14日')
      

  7.   

    --如果年月日字段为字符型
    select * 
    from tb
    where cast(年+'-'+月+'-'+日 as datetime)>='2008-2-3'
        and cast(年+'-'+月+'-'+日 as datetime)<='2009-12-14'