Solved

select current quarter in SQL query  based on company fiscal year

Posted on 2013-10-25
3
1,847 Views
Last Modified: 2013-10-25
Hello, experts,

How do I select the current quarter based on company fiscal year.  I defined the quarters as follow:
 
    CASE
        WHEN MONTH(getdate()) BETWEEN 6  AND 8  THEN 'Q1 - '
        WHEN MONTH(getdate()) BETWEEN 9  AND 11  THEN 'Q2 - '  
        WHEN MONTH(getdate()) BETWEEN 12  AND 2  THEN 'Q3 - '  
        WHEN MONTH(getdate()) BETWEEN 3 AND 5 THEN 'Q4 - '  
    END as Qtr

I need passing the current Qtr to a query:

Select * from Margin where createdon =current Qtr


Thank you very much in advance.
0
Comment
Question by:sharon2011
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39600406
Dang dang dang dang dang.  

I have an article there on Date Fun, Part One:  Build your own SQL calendar table to perform complex date expressions, which walks you through how to handle goofy-riffic calculations involving days by storing them in a table.

The 'Part Two' is how to handle fiscal years, which isn't ready for the world yet.
I take it you're dealing with crop year?

In your case, in what format is the createdon values?  Based on that we can kick out some T-SQL, and I'm guessing it'll be a nested CASE block based on what you already have, that handles the year component.
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39600437
does it need to be a query:

Most efficient way to do it would be to create a parameter:

@startMonth;

select
@startMonth =
                WHEN MONTH(getdate()) BETWEEN 6  AND 8  THEN 6
                WHEN MONTH(getdate()) BETWEEN 9  AND 11 THEN 9
                WHEN MONTH(getdate()) BETWEEN 12  AND 2  THEN 12
                WHEN MONTH(getdate()) BETWEEN 3  AND 5  THEN 3

select * from Margin 
where 
-- normal cases
(
  month(createdon) between @startmonth and @startmonth + 2 and @startmonth != 12
       and
  year(createdon) = year(getdate)
)
   OR
(
--year wrapping
   @startmonth = 12
    AND
    (
   -- It was created in decemeber and the year is based on where I'm at currently
    month(createdon) = 12 and year(createdon) =  case when month(getdate()) in (1,2) then year(getdate()) - 1 else year(getdate()) end
    Or
  -- it was created in Jan/Feb and the year is based on where I'm at currently
   month(createdon) in (1,2) and year(createdon) = case when month(getdate()) in (1,2) then year(getdate()) else year(getdate()) + 1 end
    )
 
  
)

Open in new window

0
 

Author Closing Comment

by:sharon2011
ID: 39601520
ged325,

I just made my sql query work beautifully.

Thank you very much
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to Install SSIS without the SQL Server CD 3 46
Update in Sql 7 30
Syntax using Declare 4 38
Excel conversion issue with Sql server 14 50
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

21 Experts available now in Live!

Get 1:1 Help Now