When programming stored procedures, functions, or just writing queries for Microsoft SQL Server databases, we often need date variables set to the beginning or end of a day, week, month or year. This is commonly done by concatenating strings – such as:
SET @InputDate = '4/1/2004 ' + '00:00:00'
That works but is pretty lame. This approach becomes downright unfriendly when you need the first day of a particular week or some such. Follow along and I’ll reveal the easy, T-SQL way over the coming couple of weeks.
Let’s start with the easy scenarios; the beginning of a day, month or year.
Function that returns a date set to the first day of the month and first second of the day using the input date as the starting point. When 5/15/2004 is input, the output will be 5/1/2004 00:00:00.
Function that returns a date set to the first day of the year and first second of the day using the input date as the starting point. When 5/15/2004 is input, the output will be 1/1/2004 00:00:00.
Function that returns a date set to the first second of the day using the input date as the starting point. When 5/15/2004 is input, the output will be 5/15/2004 00:00:00.
Leave a Reply