Solved

T-SQL query - List this financial years transactions

Posted on 2012-04-10
4
376 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
ID: 37831504
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
ID: 37832375
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:
Scott Pletcher earned 500 total points
ID: 37832671
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
ID: 37834681
Thanks for all the comments / ideas - all work but Scotts works best in my case.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL NULL vs Blank 26 36
Need help on t-sql 2012 10 54
Access Crosstab Query with Multiple Values 4 32
LAG_ROWID - how do I get the right order using this query? 2 3
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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 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