473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine Quarter End and Beginning Dates

Hello,

I have a query that I would like to schedule in DTS. The criteria of
this query checks for records in the table that are within the current
quarter. Here is what I have.

WHERE submit_date BETWEEN '01/01/2005' AND '03/31/2005'

I would like to dynamically generate the Quarter End and Quarter
Beginning dates within my where clause based on the date that DTWS
package is being executed on. Can anyone show me how this can be
accomplished?
Thank You.

Jul 23 '05 #1
3 14519

"Matt" <ma***********@ manning-napier.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hello,

I have a query that I would like to schedule in DTS. The criteria of
this query checks for records in the table that are within the current
quarter. Here is what I have.

WHERE submit_date BETWEEN '01/01/2005' AND '03/31/2005'

I would like to dynamically generate the Quarter End and Quarter
Beginning dates within my where clause based on the date that DTWS
package is being executed on. Can anyone show me how this can be
accomplished?
Thank You.


Quick and dirty solution - see DATEPART in Books Online.

The longer answer is that using DATEPART might not be good for performance
(applying a function to a column prevents MSSQL using an index on that
column), so you may need another approach. One would be to write a stored
proc to return the first and last days of the current quarter, so you can
put them in variables and use them in your query; another would be to create
a calendar table (which is very useful anyway) and join on it in your query.

A couple of other small points - BETWEEN with datetime columns can give you
unexpected results if you don't allow for the time portion. In your case,
this is probably safer:

where submit_date >= '20050101' and submit_date < '20050401'

Also, try to use the YYYYMMDD date format if possible - it will always be
interpreted correctly by MSSQL regardless of client or server settings. More
information here:

http://www.karaszi.com/sqlserver/info_datetime.asp

Simon
Jul 23 '05 #2
> WHERE submit_date BETWEEN '01/01/2005' AND '03/31/2005'

I would like to dynamically generate the Quarter End and Quarter
Beginning dates within my where clause based on the date that DTWS
package is being executed on. Can anyone show me how this can be
accomplished?


The easy way to do this is to set up a dates table that has columns for
quarter and year and then join
e.g. If you have a table: dates
D as datetime, Year as integer, Quarter as integer
20050101, 2005, 1
20050102, 2005, 1
....
20051231, 2005, 4

And then in your query:
Join (select Quarter, Year from dates where d = CONVERT(getdate (), datetime,
112) as t
Join dates on submitdate = d
where d.Quarter = t.Quarter and d.year = t.Year

The Hard Way is to calculate it in line:

If you want current quarter then:
WHERE submit_date BETWEEN
CAST(Year(GetDa te()) as varchar(4)) + Right('0' +
CAST((Month(Get Date())-1) / 3 * 3 + 1 as varchar(2)),2) + '01' AND
CAST(Year(GetDa te()) as varchar(4)) + Right('0' +
CAST((Month(Get Date())-1) / 3 * 3 + 4 as varchar(2)), 2) + '01'

Last quarter is harder:
WHERE submit_Date BETWEEN
CASE WHEN Month(GetDate() ) < 4 THEN
CAST(Year(GetDa te()) - 1 as varchar(4)) + '0901'
ELSE
CAST(Year(GetDa te()) as varchar(4)) + Right('0' +
CAST(Month(GetD ate()-1) / 3 * 3 - 2 as varchar(2)),2) + '01'
END
AND
CASE WHEN Month(GetDate() ) < 4 THEN
CAST(Year(GetDa te()) as varchar(4)) + '0101'
ELSE
CAST(Year(GetDa te()) as varchar(4)) + Right('0' +
CAST(Month(GetD ate()) / 3 * 3 + 1 as varchar(2)), 2) + '01'
END


Jul 23 '05 #3
On 30 Mar 2005 07:27:20 -0800, Matt wrote:
Hello,

I have a query that I would like to schedule in DTS. The criteria of
this query checks for records in the table that are within the current
quarter. Here is what I have.

WHERE submit_date BETWEEN '01/01/2005' AND '03/31/2005'

I would like to dynamically generate the Quarter End and Quarter
Beginning dates within my where clause based on the date that DTWS
package is being executed on. Can anyone show me how this can be
accomplished ?
Thank You.


Hi Matt,

In addition to the answers Simon and James gave, here's a quick formula
to calculate the first and last date of the quarter:

declare @test datetime
set @test = '20051201'
SELECT DATEADD(quarter , DATEDIFF(quarte r, '20000101', @test),
'20000101'),
DATEADD(quarter , DATEDIFF(quarte r, '20000101', @test) + 1,
'19991231')
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3966
by: p175 | last post by:
People, I have an ESE 8.2.2 database running on win2k server . I need to do some system testing that requires I reset the dates back a couple of years then progressivily move it forward to current date. The last time I tried this the database got very upset and the date functions such as DAYOFWEEK_ISO, YEAR, WEEK, QUARTER etc got all screwed up and failed. I subsequently had to completely drop the database, recreate from scratch and...
2
1376
by: Samaba via AccessMonster.com | last post by:
I need to create a calculated control which will count the number of cases if the is "open" and the is "a" for a calendar quarter. I have been struggling with which formula would work best: dcount dsum sum(iif()) What I have working so far: =DCount("","","='a' AND ='open'")
8
11887
by: Dominique Vandensteen | last post by:
I have a datetime and want to format it to "quarter year" so 20 december 2003 should give: "4-2003" is this possible? I don't find a format character for quarter :-( Dominique
3
2862
by: RD | last post by:
Say Company's financial year starts October First and say we are now March 17th. How do you determine which quarter of the Financial year - not the actual year - March 17th is in. Thanks for any help, Bob
1
2081
by: mnoland | last post by:
I relay need some help I am looking to find every quarter between 2 dates here is an example of what i am trying to do data table client sbegin send 499 1-1-2000 6-1-2001 499 12-12-05 12-21-05 499 10-5-06 12-15-06 499 12-1-06 12-10-06
2
7658
benchpolo
by: benchpolo | last post by:
First Day of the QUARTER select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) Question: How do I get the last DAY of the QUARTER? For example: 1st Quarter is 01/01/2008 to 03/31/2008. I am having difficulties finding the last day of the quarter.
2
4318
by: RZ15 | last post by:
Hi guys, I'm really drawing a blank here for how to deal with fiscal months in my monthly sales/receipts reports. My issue is that calculating the months is not as simple as saying 'if the invoice date is january then sum sales' because the first period may include the end of december or even the beginning of february and not all of january. To help me with my problem, it's probably helpful to not think of it as fiscal months but think of...
1
2538
beacon
by: beacon | last post by:
Hi everybody, The title probably won't do this post justice, so I apologize ahead of time. Here's what I'm trying to accomplish...I have a report currently that shows totals for a date range that the user enters through a form. Typically, the user will enter a fiscal quarter for the date range (max of 92 days), as that's our normal reporting period for this particular report. Just for reference, our fiscal year starts in September. My...
5
6500
by: yappy77 | last post by:
I have a query with an IIF statement that returns #ERROR. The results I want are if the requalify date is between 1/1/"year does not matter" and 3/31/"year does not matter" then First Quarter. Below is my statement: Badge Expiration Quarter: IIf(="1/1/yy" And "3/31/yy","First Quarter",IIf(="4/1/yy" And "6/30/yy","Second Quarter",IIf(="7/1/yy" And " 9/1/yy","Third Quarter",IIf(="10/1/yy" And "12/31/yy","Fourth Quarter","NA")))) I am...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.