Solved

Help with Date query

Posted on 2013-06-06
5
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

724 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