示例:
Table A
From_Date To_Date
2016-02-22 2016-02-29
Output
-------
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-27
2016-02-28
输出应该是这样的。
两列都在同一个表A中。
我想显示这两列之间的日期。
答案 0 :(得分:0)
你的意思是:
select *
from table_name
where from_date >= '2016-02-22' and to_date <= '2016-05-16'
答案 1 :(得分:0)
如果From_Date
和To_Date
是变量:
DECLARE @From_Date date = '2016-02-22',
@To_Date date = '2016-05-16'
SELECT *
FROM YourTable
WHERE DateField between @From_Date and @To_Date
如果没有,那么:
SELECT *
FROM YourTable
WHERE DateField between '2016-02-22' and '2016-05-16'
在另一张表中:
SELECT y.*
FROM YourTable y
INNER JOIN TableWithDate d
ON y.DateField between d.From_Date and d.To_Date
通过递归CTE获得您想要的输出(假设[Table A]
中的字符串为'2016-02-22'
,'2016-02-29'
为From_Date
,To_Date
):< / p>
;WITH recurs AS (
SELECT DATEADD(day,1,From_Date) as date_, To_Date
FROM [Table A]
UNION ALL
SELECT DATEADD(day,1,date_), To_Date
FROM recurs r
WHERE date_ < DATEADD(day,-1,To_Date)
)
SELECT date_
FROM recurs
输出:
date_
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-27
2016-02-28