Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SELECT STATEMENT relating to date, year, etc...

Posted on 2007-07-26
5
Medium Priority
?
214 Views
Last Modified: 2010-03-19
Hi,
I am running "SELECT" statement to output sales order year nad ordered date by quarterly using following statement:

SET DATEFORMAT mdy
SELECT DATEPART(yy, OrderReceivedDate) AS OrderYear,
             'Q' + DATENAME(qq, OrderReceivedDate) AS OrderQtr

This results in following output:

1/1/2007 thru 3/31/2007 = (OrderQtr = Q1) and (OrderYear = 2007)
4/1/2007 thru  6/30/2007 = (OrderQtr = Q2) and (OrderYear = 2007)
and so on (In accouting term Quarter is broken by "Physical Year").

However, my client's Q1 does not start on Physical Year (1/1/2007) but rather starts on 10/1/2006. So 4 quarter for 2007 is following:

10/1/2006 - 12/31/2006 = Q1 for year 2007
1/1/2007 - 3/31/2007 = Q2 for year 2007
4/1/2007 -  6/30/2007 = Q3 for year 2007
7/1/2007 - 9/30/2007 = Q4 for year 2007

So my question is how can I run select statement using similar to below statement to accomplish above Calendar year which starts on 10/1/yyyy and ends on 9/30/yyyy?

SET DATEFORMAT mdy
SELECT DATEPART(yy, OrderReceivedDate) AS OrderYear,
             'Q' + DATENAME(qq, OrderReceivedDate) AS OrderQtr

Thank you in advance,

CN
0
Comment
Question by:CyberNerd
  • 3
5 Comments
 
LVL 15

Expert Comment

by:derekkromm
ID: 19577821
SET DATEFORMAT mdy
SELECT DATEPART(yy, OrderReceivedDate) AS OrderYear,
             'Q' + DATENAME(qq, dateadd(month, 3, OrderReceivedDate)) AS OrderQtr

0
 
LVL 15

Expert Comment

by:derekkromm
ID: 19577828
sorry, mistake there:

SET DATEFORMAT mdy
SELECT DATEPART(yy, dateadd(month, 3, OrderReceivedDate)) AS OrderYear,
             'Q' + DATENAME(qq, dateadd(month, 3, OrderReceivedDate)) AS OrderQtr
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 19577834
actually, this is likely a lot better. sorry for the multiple comments:

SET DATEFORMAT mdy
SELECT DATEPART(yy, dateadd(qq, 1, OrderReceivedDate)) AS OrderYear,
             'Q' + DATENAME(qq, dateadd(qq, 1, OrderReceivedDate)) AS OrderQtr
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19577843
SELECT YEAR(OrderReceivedDate) + (CASE WHEN MONTH(OrderReceivedDate) >= 10 THEN 1 ELSE 0 END) AS OrderYear,
    'Q' + (CASE MONTH(OrderReceivedDate) WHEN >= 10 THEN 1 WHEN <=3 THEN 2 WHEN <=6 THEN 3 ELSE 4 END) AS OrderQtr
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19577895
SELECT DATEPART(yy, OrderReceivedDate) AS OrderYear,
             REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('Q' + DATENAME(qq, OrderReceivedDate),'1','0'),'2','1'),'3','2'),'4','3'),'0','4') AS OrderQtr
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…
Suggested Courses

810 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