Solved

Need help with Date query -- T-SQL

Posted on 2013-06-03
12
434 Views
Last Modified: 2013-06-09
Hello Experts,

I need your help in writing a query  to populate "On a given date I want the last month of the previous quarter from the column "CalendarDate".

See below:

CalendarDate
 For Example:  If I run the query today. i.e., 2013-06-03, I should get the date "2013-03-01"

If I run the query for a specific date instead of today, for example: "2013-07-10" , I should get "2013-06-01"

Please let me know if my question is not clear enough!

Thanks in advance
0
Comment
Question by:ravichand-sql
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
Comment Utility
This will do:

declare @dt date
set @dt = '20130701'
select case when datepart(q, @dt) = 1 then convert(date, convert(varchar, datepart(yy, @dt) - 1) + '1201') 
       else convert(date, convert(varchar, datepart(yy, @dt)) + replace(str((datepart(q, @dt)-1) * 3, 2), ' ', '0') + '01') end

Open in new window


SQL Fiddle
0
 
LVL 1

Expert Comment

by:Ray Phan
Comment Utility
Hi !
Please try this

 
select CAST(CAST(dPart.yyyy AS varchar)+ '-' + CAST(dPart.mm AS varchar)+ '-' + CAST(dPart.dd AS varchar) AS DATE) from (select datepart(yy, CalendarDate) as yyyy, (datepart(qq, CalendarDate)-1)*3 as mm, '01' as dd from YourTableName) as dPart

Hope this help.
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
@khiemp: Have you tested it for the CalendarDate in the first quarter?
0
 
LVL 9

Expert Comment

by:sarabhai
Comment Utility
try this

SELECT FirstDayOfQuarter = DATEADD(quarter,DATEDIFF(quarter,1,GETDATE()), -DAY(DATEDIFF(quarter,1,GETDATE()))-1 )
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
Comment Utility
deduct modulus of month % 3 , and deduct (one less than) the day , of a date
declare @dt date
set @dt = '20130721'
select dateadd(day,-datepart(day,@dt) + 1,dateadd(month,- datepart(month,@dt) % 3,@dt))
;

select
  adatefield
, dateadd(day,-datepart(day,adatefield) + 1,dateadd(month,- datepart(month,adatefield) % 3,adatefield))
from yourtable
;

Open in new window

e.g.
2013-01-21
month = 7
modulus 7 % 3  = 1, deduct 1 month = June, ( nb & this also sorts out the year)

day = 21
deduct (one less than) 21 = (21-20) = 1

see: http://sqlfiddle.com/#!3/65395/2
0
 
LVL 9

Expert Comment

by:sarabhai
Comment Utility
for custom date

DECLARE @dt DATETIME
SET @dt=GETDATE()
SELECT FirstDayOfQuarter = DATEADD(quarter,DATEDIFF(quarter,1,@dt), -DAY(DATEDIFF(quarter,1,@dt))-1 )
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:Ray Phan
Comment Utility
@chaau:Thanks for you notice, so I have modified my query string :


select CAST((CASE WHEN dPart.mm < 1 THEN CAST(dPart.yyyy-1 AS varchar ) ELSE CAST(dPart.yyyy AS varchar) END)+ '-' +(CASE WHEN dPart.mm < 1 THEN '12' ELSE CAST(dPart.mm AS varchar) END  )+ '-' + CAST(dPart.dd AS varchar) AS DATE)
from (select datepart(yy,CalendarDate) as yyyy, (datepart(qq,CalendarDate)-1)*3 as mm, '01' as dd from YourTableName) as dPart
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
http://sqlfiddle.com/#!3/caa6e/1

3 of the 4 compared (ID: 39217882 not included as it needed more effort to include, sorry)
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
@PortletPaul: cool, thanks for the effort. Looks like I am winning so far. However, I like your approach more, as I think it will be better performance wise. I think, it is very easy to fix.
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
I noticed the difference too :)
I was quite happy with mine, but it may need tweaking... feel free to play with it

anyone can join in :)
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
@PortletPaul: your will work if you do this:

dateadd(day, - datepart(day, adatefield) + 1, dateadd(month, - (datepart(month, adatefield) + 2) % 3 - 1, adatefield))

Open in new window


See last column: http://sqlfiddle.com/#!3/caa6e/6
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
@chaau
( ... + 2) ... - 1
amazing what a two integers will do :)
nicely done
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now