-2

Example:

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 

The output should come like this.

Both columns are in same table A.

I want to display the dates that are in between these two columns.

Sakthi
  • 9
  • 1
  • 6
  • 1
    how about using google before asking on stackoverflow? – Tikkes Jun 17 '16 at 10:42
  • 1
    Possible duplicate of [t-sql get all dates between 2 dates](http://stackoverflow.com/questions/3946121/t-sql-get-all-dates-between-2-dates) – gotqn Jun 17 '16 at 10:44
  • It isn't clear what you are asking. Can you edit your question to include all the relevant columns from your table? Can you also include some sample rows? Finally can you show us the output you would expect that sample to return? See the [help pages](http://stackoverflow.com/help/mcve) for more on providing helpful sample data. – David Rushton Jun 17 '16 at 11:22
  • This is marked as duplicate. However, I would recommend the answer from Martin Smith over the select answer. A tally table is much more efficient than a recursive cte for this type of thing. – Sean Lange Jun 17 '16 at 13:11

2 Answers2

0

You mean:

select *
from table_name
where from_date >= '2016-02-22' and to_date <= '2016-05-16'
ThiepLV
  • 1,219
  • 3
  • 10
  • 21
0

If From_Date and To_Date are variables:

DECLARE @From_Date date = '2016-02-22',
        @To_Date date = '2016-05-16'

SELECT *
FROM YourTable
WHERE DateField between @From_Date and @To_Date

If not then:

SELECT *
FROM YourTable
WHERE DateField between '2016-02-22' and '2016-05-16'

In another table:

SELECT y.*
FROM YourTable y
INNER JOIN TableWithDate d
    ON y.DateField between d.From_Date and d.To_Date

To get output you want with recursive CTE (assuming that in [Table A] is string with '2016-02-22', '2016-02-29' values for From_Date, To_Date):

;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

Output:

date_
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-27
2016-02-28
gofr1
  • 15,741
  • 11
  • 42
  • 52