Calculate Start & End Date from Given Date

Posted: August 11, 2010 in SQL Server 2005

Get the the start and end date values from a given date, use the functions below:

-- Declare variables
DECLARE @ldt_start_date DATETIME;
DECLARE @ldt_end_date   DATETIME;
DECLARE @ldt_today      DATETIME;

-- Initiliase variables
SET @ldt_today      = dbo.dbf_system_date();
SET @ldt_start_date = DATEADD(MONTH, DATEDIFF(MONTH, 0, @ldt_today + 1), 0);
SET @ldt_end_date   = DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, @ldt_today) + 1, 0));

-- Print the results
PRINT @ldt_today;
PRINT @ldt_start_date;
PRINT @ldt_end_date;
Advertisements

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