SQL Scrapbook

Short and simple SQL Server posts

Archive for October 2010

SQL Date Cheat Sheets

leave a comment »

This post will show the commonly used queries to play with date functions in SQL Server. And yes i hope to update this list frequently.
Currently the following script has queries to:
Select two digit month, Select the first and last day of the month…

 --query to select month in two digits
SELECT	[TwoDigitMonth]
		= RIGHT('0' + RTRIM(MONTH(GETDATE())), 2)
--select first day of month
 SELECT	[FirstDayofMonth]
		= CONVERT(VARCHAR(10),DATEADD(DD,-(DAY(GETDATE())-1),GETDATE()),101)
--select last day of month
 SELECT	[LastDayofMonth]
		= CONVERT(VARCHAR(10),DATEADD(DD,-(DAY(DATEADD(MM,1,getdate()))),DATEADD(MM,1,GETDATE())),101)

You can replace the GETDATE() with a variable to use this for any month.

Written by sqlscrapbook

October 20, 2010 at 7:46 pm

Posted in TSQL