如何查询特定日期范围内的所有文件?

时间:2014-12-01 16:59:05

标签: sql

我有一个带有字段的表(A,B,C,D,E,F,G,H,I)。

所有这些(A,B,C,D,E,F,G,H,I)字段均为2010-02-24至2020-01-18的日期字段。

现在我想选择2014年和2015年的所有(A,B,C,D,E,F,G,H,I)字段。

2 个答案:

答案 0 :(得分:1)

您可以使用and添加另一个where子句,然后使用between

检查某个值是否在某个范围内
...
where A between '20140101' and '20153112'
  and B between '20140101' and '20153112'
  and C between '20140101' and '20153112'
  and ...

答案 1 :(得分:0)

这不是很干净的代码,但是这会找到并记录任何一个f ... A ...我的值在@DATESTART和@DATEEND

的范围内
DECLARE @T TABLE (A DATE,B DATE, C DATE, D DATE, E DATE , F DATE, G DATE, H DATE, I DATE)
DECLARE @DATESTART DATE = '2010-02-24'
DECLARE @DATEEND DATE = '2020-01-18'

INSERT INTO @T VALUES ('2010-02-24','1986-06-27','1968-09-01','1968-01-15','1968-01-18','1986-06-27','1986-06-27','2010-02-24','1986-06-27'),

SELECT  A,B,C,D,E,F,G,H,I FROM @T
WHERE
    A BETWEEN @DATESTART AND @DATEEND
    OR
    B BETWEEN @DATESTART AND @DATEEND
    OR
    C BETWEEN @DATESTART AND @DATEEND
    OR
    D BETWEEN @DATESTART AND @DATEEND
    OR
    E BETWEEN @DATESTART AND @DATEEND
    OR
    F BETWEEN @DATESTART AND @DATEEND
    OR
    G BETWEEN @DATESTART AND @DATEEND
    OR
    H BETWEEN @DATESTART AND @DATEEND
    OR
    I BETWEEN @DATESTART AND @DATEEND