I have no idea what type of dates you are working with. Where I work, we use Julian calendar dates in our databases. I have developed a method for resolving the span of dates between to Julian dates. It took no thought and 13 lines of code.
Also, you are not going to implement such a function in SQL. Forget the database integration; it is just an abstraction layer.
I apologise for not including the code, I mentioned, but I am logging on today from an alien machine in an alien office. So far, no one has noticed my meditation robes or heard my chanting of Perl mantras.
I always code these things in either PL/SQL or PL/Perl, which both provide database integration, as they run inside the database server and not on the database client.
-- Snazzy tagline here