• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 940
  • Last Modified:

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


Server info:
MySQL 5.0.45-community-nt via TCP/IP
MySQL Client Version 5.1.11
InnoDB tables
  • 2
1 Solution
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.

One correction to my last post.  The final query needs one little tweek if you want it to display the last minute.  Below is the updated version of this query in Step 5 above....

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])+CDate(CBool([tblLog].[End]-[tblLog].[Start])/-1440)))
ORDER BY qryMin.T;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now