• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Need help with Date query -- T-SQL

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
ravichand-sql
Asked:
ravichand-sql
  • 4
  • 4
  • 2
  • +1
2 Solutions
 
chaauCommented:
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
 
Ray PhanSoftware DeveloperCommented:
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
 
chaauCommented:
@khiemp: Have you tested it for the CalendarDate in the first quarter?
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
sarabhaiCommented:
try this

SELECT FirstDayOfQuarter = DATEADD(quarter,DATEDIFF(quarter,1,GETDATE()), -DAY(DATEDIFF(quarter,1,GETDATE()))-1 )
0
 
PortletPaulCommented:
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
 
sarabhaiCommented:
for custom date

DECLARE @dt DATETIME
SET @dt=GETDATE()
SELECT FirstDayOfQuarter = DATEADD(quarter,DATEDIFF(quarter,1,@dt), -DAY(DATEDIFF(quarter,1,@dt))-1 )
0
 
Ray PhanSoftware DeveloperCommented:
@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
 
PortletPaulCommented:
http://sqlfiddle.com/#!3/caa6e/1

3 of the 4 compared (ID: 39217882 not included as it needed more effort to include, sorry)
0
 
chaauCommented:
@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
 
PortletPaulCommented:
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
 
chaauCommented:
@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
 
PortletPaulCommented:
@chaau
( ... + 2) ... - 1
amazing what a two integers will do :)
nicely done
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now