Common date functions in SQL

Useful for checking that the date range is less than the given end date.

DATEADD(day,DATEDIFF(day,0,@endDate),1)

Assuming you are given a date that can fall on any day within a month:

Get the first day of the month containing @startDate:

set @currentMonthStartDate = DATEADD(month, DATEDIFF(month, 0, @startDate), 0)

Get the last day of the month containing @startDate:

set @currentMonthEndDate = DATEADD(day,-1, DATEADD(month, DATEDIFF(month, 0, @startDate) + 1, 0) );

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s