Solved

Help with Date query

Posted on 2013-06-06
5
368 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now