Solved

Help with Date query

Posted on 2013-06-06
5
377 Views
Last Modified: 2013-06-09
Hello Experts!!

I need your help in writing a query to populate a date for the below scenario.

For a given quarter date, I want to get first day of the first month of the quarter and last day of the last month of the quarter.

For Example... If my quarter date is : 03-01-2013, then my startdate should be 01-01-2013 and enddate should be 03-31-2013

Likewise,

QuarterDate:                  FirstDate:                             LastDate:
03-01-2013                     01-01-2013                          03-31-2013
06-01-2013                     04-01-2013                          06-30-2013

Thanks in advance...!!!


CalendarDate
0
Comment
Question by:ravichand-sql
5 Comments
 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 166 total points
ID: 39227379
This should do the trick:

SELECT  CalendarDate,
        CONVERT(datetime, CONVERT(varchar, (DATEPART(q, CalendarDate) * 3) - 2) + '/1/' + CONVERT(varchar, YEAR(CalendarDate)))
                    AS QtrBeg,
        DATEADD(m, 1, CONVERT(datetime, CONVERT(varchar, DATEPART(q, CalendarDate) * 3) + '/1/' + CONVERT(varchar, YEAR(CalendarDate)))) - .00000005
                    AS QtrEnd
FROM    YourTableName WITH (NOLOCK)

Open in new window

I had to fix the code a little bit, so if you grabbed it before you see this message, please grab it again.
0
 
LVL 24

Expert Comment

by:chaau
ID: 39227452
Just a small note: this query will only work in America, or in the countries with mm/dd/yyyy date format. Will not work in Europe
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 167 total points
ID: 39227655
This should do it:
SELECT	DATEADD(quarter, DATEPART(quarter, QuarterDate) - 1, DATEADD(day, 1 - DATEPART(dayofyear, QuarterDate), QuarterDate)),
	DATEADD(day, -1, DATEADD(quarter, DATEPART(quarter, QuarterDate), DATEADD(day, 1 - DATEPART(dayofyear, QuarterDate), QuarterDate)))

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39227663
This is how I tested it:

DECLARE @QuarterDate date 

SET @QuarterDate = '20130301'
SELECT	DATEADD(quarter, DATEPART(quarter, @QuarterDate) - 1, DATEADD(day, 1 - DATEPART(dayofyear, @QuarterDate), @QuarterDate)),
	DATEADD(day, -1, DATEADD(quarter, DATEPART(quarter, @QuarterDate), DATEADD(day, 1 - DATEPART(dayofyear, @QuarterDate), @QuarterDate)))

SET @QuarterDate = '20130601'
SELECT	DATEADD(quarter, DATEPART(quarter, @QuarterDate) - 1, DATEADD(day, 1 - DATEPART(dayofyear, @QuarterDate), @QuarterDate)),
	DATEADD(day, -1, DATEADD(quarter, DATEPART(quarter, @QuarterDate), DATEADD(day, 1 - DATEPART(dayofyear, @QuarterDate), @QuarterDate)))

Open in new window

0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 167 total points
ID: 39227859
DECLARE @QuarterDate DateTime
SET @QuarterDate = '2013-06-01'	
SELECT FirstDate=DATEADD(QUARTER,DATEDIFF(QUARTER,0,@QuarterDate),0)
      ,LastDate=DATEADD(QUARTER,DATEDIFF(QUARTER,0,@QuarterDate)+1,-1)

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question