显示两个字段的日期

时间:2012-08-21 04:57:35

标签: sql sql-server-2008 tsql date

我有一个包含两列startdateenddate(类型为DATETIME)的表格。当我传递两个参数即开始日期和结束日期时,我需要显示两列中两个日期之间的所有日期。

即..我有这张桌子:

 startdate  enddate
 ---------------------
 6/1/2012   6/7/2012
 6/5/2012   6/9/2012
 6/10/2012  6/15/2012

当我传递6/3/20126/20/2012这两个日期时,我想显示此结果集:

  6/5/2012
  6/7/2012
  6/9/2012
  6/10/2012
  6/15/2012

提前致谢

2 个答案:

答案 0 :(得分:1)

UNION SubQuery中的所有日期。尝试,

SELECT allDates
FROM
(
    SELECT startdate as allDates
    FROM table
    UNION
    SELECT endDate as allDates
    FROM table
) a
WHERE allDates BETWEEN '6/3/2012' AND '6/20/2012'

答案 1 :(得分:0)

MySql示例语法:

CREATE TABLE TEST(STARTDATE DATE,ENDDATE DATE);

INSERT INTO TEST VALUES (NOW(), NOW());
INSERT INTO TEST VALUES ('2012-08-19', '2012-08-15');
INSERT INTO TEST VALUES ('2012-08-17', '2012-08-10');
INSERT INTO TEST VALUES ('2012-08-23', '2012-08-21');

SELECT STARTDATE FROM TEST WHERE STARTDATE BETWEEN '2012-08-15' AND '2012-08-23' 
UNION SELECT ENDDATE FROM TEST WHERE ENDDATE BETWEEN '2012-08-15' AND '2012-08-23' 
ORDER BY STARTDATE;