Solved

T-SQL query - List this financial years transactions

Posted on 2012-04-10
4
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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