Link to home
Start Free TrialLog in
Avatar of jmokrauer
jmokrauerFlag for United States of America

asked on

how to populate a table with dates

I would like a select statement that creates all the dates & times (down to the minute level) between two different dates.  All dates and times would be in the following format:

'2007-08-01 12:25'

So if the start time was '2007-08-01 11:58' and the end time was '2007-08-02 00:02' then the statement should return the following:

2007-08-01 11:58
2007-08-01 11:59
2007-08-02 00:00
2007-08-02 00:01
2007-08-02 00:02

Thanks!


Server info:
MySQL 5.0.45-community-nt via TCP/IP
MySQL Client Version 5.1.11
InnoDB tables
Avatar of hernst42
hernst42
Flag of Germany image

AFAIk not possible as there is no pseudo table where you could select from which would generate dummy records. You might write a stored procedure that creates the records or use a scripting language (like php) to do this.
Actually there is a way to do this and you'll be pleased to know it can be done w/o relying a massive set of data in some temporary table to do it.  You will need to use a table but it can be quite small.

For the sake of simplicity I'm going to assume you're solution is only looking to list the minutes within a 24 hour period.  Although this solution can be expanded to provide a list of minutes accross many days, such a solution would require a little more explanation (abliet not too terribly much) but I presume you're lists aren't going to be that long.  If they are just let me know and I can assist you through the additoinal changes required.

Now for the solution

Step 1:  Create a table name tblX10 with One field and name it X10.  Make the data type Byte.  Set the data validation rule to be as follows...

Between 0 And 9

Step 2: Add 10 records (values) to the tblX10.  Create 10 records with values 0 - 9.   (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9)

Step 3: Create a query using the SQL code below and name it qryMin.

SELECT CDate(CDbl(tblX1000.X10 & tblX100.X10 & [tblX10].[X10] & tblX1.X10)/1440) AS T
FROM tblX10 AS tblX1, tblX10, tblX10 AS tblX100, tblX10 AS tblX1000
WHERE ((([tblX1000].[X10] & [tblX100].[X10] & [tblX10].[X10] & [tblX1].[X10])<=1440));

Step 4: Without knowing the table structure for your particular situation I'll take the liberty of assigning names to the table and it's fields.  Let's give your table the name of "tblLog" and give it 2 date fields named [Start] and [End] respectively.  Of course the actual names should not include the brakets or quotes.  

Step 5: The query below will avail the list you desire.  Although you may have to update the name "tblLog" to match that of you're actual table and/or update the date field names [Start] and [End] according to the name you're actually using everything else about the query should work as is.

SELECT Format([tblLog].[Start]+[qryMin].T,"dd/mm/yyyy hh:nn AM/PM") AS Minutes
FROM tblLog, qryMin
WHERE (((qryMin.T)<=CDate([tblLog].[End]-[tblLog].[Start])))
ORDER BY qryMin.T;

*******************************************

Believe it or not that's all there is to it.  The result will be a table of minutes that start and end between the Dates/Times provided in your table [Start] and [End] respecively.  

Note: The above solution presumes you will not need to list more than 24 hours worth of minutes and that the Start Date preceeds the End Date.  Other than that it'll work as is.

Well there you go, just let me know if you have any questions.


ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial