Solved

T-SQL query - List this financial years transactions

Posted on 2012-04-10
4
373 Views
Last Modified: 2012-04-11
I would like to list all transactions this financial year (from 31 March).

I have tried something like:-

SELECT     InvDate
FROM         CustInv
WHERE     (InvDate > DATEADD(m, CASE WHEN datepart(m, Getdate()) > 3 THEN 4 ELSE - 8 END - DATEPART(M, GETDATE()), DATEADD(d, 1 - DATEPART(d, GETDATE()),
                      GETDATE())))

However although the date I am getting is 1st April it also includes hours, minutes and seconds - In some cases InvDate is like 2012-04-01 00:00:00.000 and if I am comparing this to above it is not going to include this date.

I guess I could subtract hours, minutes and nanoseconds in the same way as above but thought there might be an easier way.
0
Comment
Question by:donhannam
4 Comments
 
LVL 12

Expert Comment

by:Harish Varghese
Comment Utility
Since your financial year always start on April 1, you just have to find the proper year - current year or last year.

SELECT   InvDate
FROM     CustInv
WHERE  InvDate >= CONVERT (SmallDateTime, CASE WHEN MONTH(GetDate()) > 3 THEN Convert (varchar(4), YEAR(GetDate())) + '/04/01'
                            ELSE Convert (varchar(4), YEAR(GetDate()) -1) + '/04/01' END)

Note: If InvDate column in CustInv table is DateTime or SmallDateTime, then above CONVERT can be removed to make it look simpler.

Thanks,
Harish
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
It might be best if you do it in a separate step (no points please) as in:

DECLARE @FromDate date =       
            CASE
                  WHEN MONTH(GETDATE()) BETWEEN 1 AND 3 THEN CAST(YEAR(GETDATE()) AS char(4)) + '0401'
                  ELSE CAST(YEAR(GETDATE() + 1) AS char(4)) + '0401'
            END
DECLARE @ToDate date = DATEADD(DAY, -1, DATEADD(Year, 1, @FromDate))

...
      
WHERE     InvDate BETWEEN @FromDate AND @ToDate
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
Easiest to me is to force the time to 00:00:00.000:


WHERE     (InvDate > DATEADD(m, CASE WHEN datepart(m, Getdate()) > 3 THEN 4 ELSE - 8 END - DATEPART(M, GETDATE()), DATEADD(d, 1 - DATEPART(d, GETDATE()),
                      DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))))  --<<-- reset time on GETDATE() to 00:00:00.000
0
 

Author Closing Comment

by:donhannam
Comment Utility
Thanks for all the comments / ideas - all work but Scotts works best in my case.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

11 Experts available now in Live!

Get 1:1 Help Now