I'm trying to get day count of from the first day of the current year till today
For example:
Today = 24/07/2023
FirstDate = 01/01/2023
LastDate = 24/07/2023
The result is 204 days
Is it possible to get the result with just a query (no stored procedure)?
You can use the built-in function
DATEDIFFfor this:Result: 204
dbfiddle: https://dbfiddle.uk/wbKAN8Mt
If you want to dynamically find out the first day of the year from the date, you can use the
FIRST_DAYfunction, but this requires Firebird 4.0:Result:
dbfiddle: https://dbfiddle.uk/gl9n-m0p
As pointed out in the comments by user13964273, if you want to know the days since the start of the year, you can also use
EXTRACT(YEARDAY FROM <value>), because 0 is 1st of January, etc, it will produce the same result asdatediff(day, first_day(of year from date_val), date_val):Modified example:
dbfiddle: https://dbfiddle.uk/NVimzbAz