Full review (do not merge)

I *think* you should be able to use SQL2011 standard functions to get the behavior:

http://en.wikibooks.org/wiki/SQL_Dialects_Reference/Functions_and_expressions/Date_and_time_functions