Home > Blockchain >  Fiscal year from getdate in SQL
Fiscal year from getdate in SQL

Time:03-18

I need to get the fiscal year from getdate(). My fiscal year format should be if getdate is today(18/03/2022) then the fiscal year column should show 'FY22' I used the below query to get the fiscal year Query Used:

enter image description here

CodePudding user response:

Add 6 months to the date (getdate in your example), then take the righmost two digits of the year, and concatenate 'FY' in front of it The actual expression is rdbms dependent -you have not tagged; but for SQLServer:

'FY' right(cast(year(dateadd(month,6,getdate())) as char(4)), 2)

or, using a relatively recent FORMAT function making use of format providers in the Microsoft .Net library (https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings):

select format(dateadd(month,6,getdate()),'\F\Yyy')

Backslash characters cause the function to ignore any special formatting code meaning of the letters F and Y, and hence F and Y are simply passed on to the output, the remaining yy is a format specifier for two digit years.

This is, of course, if your fiscal year runs from 1-Jul to 30-Jun.

  •  Tags:  
  • sql
  • Related