Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone..!!
I have two columns....from_date and to_date having their datatype as datetime.
I want to have a list of dates falling in between from_date and to_date.

How can i do it in a query..?

Any clarification,suggestion or links are welcomed..!!
Posted

Is it that much hard Vaibhav,
Well have a look on this:

SQL
SELECT * FROM tableName WHERE DtcolumnName >= @From_Date AND DtcolumnName <= @To_Date
 
Share this answer
 
Comments
vaibhav mahajan 23-Apr-12 4:08am    
thnks prasad..!!
but..
Suppose I have a table containing only one record ...there are two columns having datatype as datetime.......from_date is having a value of 01/01/2012 and to_date having a value of 04/03/2012 ...i want a list of dates between 01/01/2012 and 04/03/2012..
hey,
try this
SQL
SELECT  distinct
      colName
  FROM tblName
  where colName between  fromDate and toDate


Hope it will help u
best Luck
Happy Coding:)
 
Share this answer
 
Comments
Mohamed Mitwalli 23-Apr-12 4:30am    
5
Hi,

Try this,
SQL
WITH date_range (calc_date) AS (
    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) - 6, 0)
        UNION ALL SELECT DATEADD(DAY, 1, calc_date)
            FROM date_range
            WHERE DATEADD(DAY, 1, calc_date) < CURRENT_TIMESTAMP)
SELECT calc_date
FROM date_range;


replace the CURRENT_TIMESTAMP with your dates..
All the best..
 
Share this answer
 
Comments
[no name] 23-Apr-12 5:50am    
What is the problem in this solution.. Could you please tell me? Why down vote?
In your case, assuming you are comparing a variable (or even a date column), Just try this one in your WHERE clause:

SQL
Where [date_TO_BE_COMPARED] BETWEEN from_date AND to_date
 
Share this answer
 
v2
Hi
Try this

SQL
WITH cteDateList AS

 (

     SELECT CAST('2012-01-01' AS DATETIME) DateValue

     UNION ALL

     SELECT DateValue + 1

     FROM    cteDateList

     WHERE   DateValue + 1 < '2013-01-01'

 )

 SELECT DateValue

 FROM    cteDateList

 OPTION (MAXRECURSION 0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900