Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following SQL. It gives the results for calendar year. I need it to give results for Financial Year (July to June). I only have one "SQL Expression" field where I can paste the code into so must be a running code if that makes sense?

CAST(SUM(CASE WHEN datediff(yyyy,getdate(),[vBoGiftsReceived].[GiftDate]) = 0 THEN [vBoGiftsReceived].[GiftAmount] ELSE 0 END) AS DECIMAL(10, 2))

Thanks!!

What I have tried:

I think I need to do something with the getdate but no idea what!
Posted
Updated 22-Sep-19 22:40pm
Comments
Herman<T>.Instance 23-Sep-19 3:09am    
SQL Injection. Always a NO GO!
What is your exact problem? Can't look in your mind
rach_303 23-Sep-19 3:36am    
Thanks for your reply. Sorry I'm only new and now sure of protocols.

In our database we use queries but its all done through a wizard builder so no actual coding. However for this particular query the SQL has been entered into a custom "SQL Expression" field, which is what I have pasted above.

The query looks at the GiftDate in the vBoGiftsReceived table and using the current system date (getdate) sums the gifts that are dated within the current calendar year. This is repeated using -1 -2 etc to look at previous calendar years.

I would like to know if there is a way to change this SQL to sum the gifts received for Financial Year, based on the giftdate and the current date. Financial year would be Jul-Jun.

Thanks!
CHill60 23-Sep-19 4:37am    
Ok, I give in. Where is the SQL Injection in the code?
Herman<T>.Instance 23-Sep-19 5:40am    
'I only have one "SQL Expression" field where I can paste the code into' .....
rach_303 23-Sep-19 7:04am    
Sorry I actually don't know what that means?!

1 solution

There is no standard approach worldwide but this apparently returns the Australian fiscal year (caveat - untested by me)

SQL
select DATEADD(dd,0, DATEDIFF(dd,0, DATEADD( mm,
 -(((12 + DATEPART(m, getDate())) - 7)%12), getDate() ) 
 - datePart(d,DATEADD( mm, -(((12 + DATEPART(m, getDate())) - 7)%12),getDate() ))+1 ) )
credit: Mouli @ accounting - Calculate fiscal year in SQL Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900