I got some comments about the Last_Day-funktion. Mainly "Why all this stringconverts" and so on. My only answer for this is "Why not?" and the main reason for the post was to describe how easy it is creating functions, working with different datatypes and manipulate strings. Well, I guess it also showed how easy it is to get something wrong. There are many ways of doing this and I have a bunch of them. I'm sorry for publish "bad" code... We all know that if we read it on the net it's true... *smiles
Anyway, to reach the last date in current month you can do this:
SELECT getdate(), DATEADD(day, 0 - DATEPART(dd, DATEADD(month, 1, getdate())), DATEADD(month, 1, getdate()))
It's the bold part that's interesting. Lets break it up!
DATEADD(month, 1, getdate()) is just one month ahead. Let's call it {today_add_one_month}
DATEPART(dd, DATEADD(month, 1, {today_add_one_month})) gives us the actual daynumber of next month. Could be called {daynumber_next_month}
Using 0 - {daynumber_next_month} as an offset, {date_offset}
DATEADD(day, {date_offset}, {today_add_one_month})
So, there you have it. I guess this is the fastest way of doing it. But with functioncalls you'll never know.