Date Serial In Access

Here the DateSerial function returns a date that is the day before the first day (1 - 1), two months before August (8 - 2), 10 years before 1990 (1990 - 10); in other words, May 31, 1980. DateSerial(1990 - 10, 8. Would it be possible to only include the date serial dd/mm/yy of the startfield, and leave the time stamp out using the function above in the function below.

  • DateSerial(Year(Date), Month(12), 0) works as the last day of the previous year in a message box What I'm trying to do is put the criteria as between the first day of the previous year and the last day of the previous year in the criteria and I keep getting Data type mismatch.
  • The TimeSerial function returns a time for 15 minutes before (-15) six hours before noon (12 - 6), or 5:45:00 A.M. TimeSerial(12 - 6, -15, 0) When any argument exceeds the normal range for that argument, it increments to the next larger unit as appropriate.
If you are a fan of DateSerial() function in Microsoft Access or like me you deal with data where you only have year and month but would like to do date calculation/manipulation, then here is a replication of this functionality for Microsoft SQL Server utilizing a user defined function.
DateSerial() takes in a number of years, months, and days and returns the appropriate date allowing you vary inputs and use negatives without having to worry about crossing over months or years. If you want more information on how the DateSerial() function worked within Access, see reference below.
Others may exist, but let's explore my version...
The Methodology
The main principle is to utilize simple DateAdd() function. The DateAdd() function takes a datepart like 'day', 'month', or 'year', etc. without the quotes (unlike MS Access); a signed integer indicating the number or days to add/subtract; the datetime to add/subtract number of datepart units from.
Basically, once a date is established by parsing the year sent to our DateSerial() function, we can simply add months and days using DateAdd().
This is very straight-forward and explained in code, so...
The Code
Adds some error handling for invalid start years has been added that can be adjusted per your own tastes.
Here is the usage:
Example usage above returns '2009-04-01 00:00:00.000' and '2008-11-01 00:00:00.000', respectively. Function
The first example, dbo.DateSerial(2009, 4, 1), simply puts in a year, month, and day. However, the second is a bit more involved.
For the second example, we start with the year 2009. Now for month, we have a -1, but what are we starting with? Well, since January is month 1, then a month of 0 would be one month prior to January; therefore, a year of 2009 and month of 0 would start us at December 2008. Now subtracting one month, we arrive at November 2008. The last parameter sets the day to be the 1st; hence our result.
Clear as mud, right!
Well, here is a look at a scenario where this may be useful:
We have a labor entry system that stores a year and month of entry that we cannot change, but we need to utilize the two values as a date to make our where clause more easily able to determine entries for the last three months. This is possible, otherwise, but can sometimes be ugly in syntax.
Date The below snippet is one solution I have used...
Now here is the same code with DateSerial...

Ms Access Dateserial

Notice we were able to use standard date function as our DateSerial() tool returns a valid datetime value.
To explore this further, see this alternative allowing us to compare dates:
Very useful.
As a good alternative, my method without DateSerial() shown earlier is very clean as well for this particular case and was developed after creating this function, but as with anything else there is always a more complex need where this can come in handy like:

Dateserial In Access


This will return the date 35 days prior to the first day of the current month in the current year!
Thanks for reading, and I hope you find my ramblings useful. Always good to have a lot of tools in the arsenal, so just giving you another with our new found DateSerial() function.
Happy coding!
Best regards,
Kevin (aka MWVisa1)
Date Serial In AccessReferences / Related Articles:

Date Serial Access Query


Dateserial Function Access

DateSerial in Access > http://office.microsoft.com/en-us/access/HA012288131033.aspx

Using Dateserial In Access Query


TimeSerial in MS SQL > http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/A_669-TimeSerial-Function-for-Microsoft-SQL-Server.html