SQL Scrapbook

Short and simple SQL Server posts

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.

Advertisements

Written by sqlscrapbook

October 20, 2010 at 7:46 pm

Posted in TSQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: