Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to select dates between two range Expand / Collapse
Author
Message
Posted Friday, March 13, 2009 9:47 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 3:52 PM
Points: 377, Visits: 685
Hi All,
I need to select dates between "02/15/2009" and "02/19/2009" how do I do that?

Thanks
Post #675350
Posted Friday, March 13, 2009 10:24 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,212, Visits: 3,232
More information please!

Do you want to select all the records in some table or view where the value of a datetime column is between two dates, or do you want to return a contiguous set of dates, i.e. one row per day between the specified dates.

Also is your date range inclusive? i.e. do you want to include rows associated with the specified end date in your resultset?
Post #675389
Posted Friday, March 13, 2009 10:35 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 3:52 PM
Points: 377, Visits: 685
I want to select dates between "02/15/2009" and "02/19/2009", one day per row like,
02/15/2009
02/16/2009
02/17/2009
02/18/2009
02/19/2009

so the end date should be included

thanks
Post #675398
Posted Friday, March 13, 2009 10:53 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,212, Visits: 3,232
You need some form of Tally table (or Numbers table). These can be permanent tables or can be generated on the fly. The Tally table I'm using below has the following structure:

CREATE TABLE dbo.Tally (
N int NOT NULL PRIMARY KEY CLUSTERED
)

There's plenty of information on Tally tables elsewhere on this site. Here's the T-SQL to return the list of sequential dates. In this case, the Tally table must contain the sequential integers between 0 and the maximum number of rows you are ever likely to require.

DECLARE @startDate datetime
DECLARE @endDate datetime

SELECT @startDate = '20090215', @endDate = '20090219'

SELECT DATEADD(day, T.N, @StartDate) AS [Date]
FROM dbo.Tally T
WHERE (T.N >= 0 AND T.N <= DATEDIFF(day, @startDate, @endDate))
ORDER BY T.N

Post #675421
Posted Friday, March 13, 2009 12:12 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 3:52 PM
Points: 377, Visits: 685
Thanks Andrew, that really worked.
Post #675488
Posted Monday, April 12, 2010 11:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 25, 2010 12:19 PM
Points: 3, Visits: 19
Hello all,

I'm really (I mean really) new to this so thanks for the info, I've learned a ton about Tally tables. However, I still don't understand how I get say a specific column of dates from a table into the Tally table?

As an example:

If I wanted to get the month begin/end from somecolumnname.tablename into the Tally table?? I guess I'm a little lost at where the Tally table gets it's data from initially
Post #901806
Posted Monday, April 12, 2010 12:41 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 27, 2014 8:55 AM
Points: 1,639, Visits: 1,985
A tally table is something that you need to generate. Here's an article about it including several scripts to create it:

http://www.sqlservercentral.com/articles/T-SQL/62867/
Post #901860
Posted Monday, July 16, 2012 6:17 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, November 14, 2014 8:13 PM
Points: 2,080, Visits: 603
If you have SQL 2005+ you can use a CTE to get the results:

DECLARE @StartDate DATETIME;
DECLARE @EndDate DATETIME;

SET @StartDate = '4/1/2012';
SET @EndDate = '6/15/2012';

;WITH
n AS (SELECT num = ROW_NUMBER() OVER (ORDER BY (SELECT 1)) FROM sys.all_columns a1 CROSS JOIN sys.all_columns a2),
dt AS (SELECT [date] = DATEADD(day, n.num - 1, @StartDate) FROM n WHERE n.num BETWEEN 1 AND DATEDIFF(day, @StartDate, @EndDate) + 1)
SELECT * FROM dt;

Post #1330466
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse