- Community Pick
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.
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.
The below snippet is one solution I have used...
Now here is the same code with 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:
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)
References / Related Articles:
DateSerial in Access > http://office.microsoft.co
TimeSerial in MS SQL > http://www.experts-exchang
by: mwvisa1 on 2010-10-08 at 05:42:28ID: 20333
Date and Time Functions (Transact-SQL)
http://msdn.microsoft.com/
As you can see in SQL 2008 and higher, you now have Date and DateTime2 which support a broader range of valid dates. Still no DateSerial function, so this is still valid.
Thanks again for reading and voting above.
Kevin