Please share your feedback on this problem. I need to calculate difference in 'years' and store it under a new column 'Age'.
While the formula works fine, it gives me incorrect output when I consider dates starting from 1st Jan of any year
For example: difference in years between 1st Jan 2019 and 31st Dec 2021 is 3 years - this includes end date in calculation. My result shows 2 years.
Here are the 2 date columns from which I am deriving the difference:
However, when I consider dates from 1st Jan - the result shows me one year less:
Here is the code I used to calculate difference:
UPDATE animals
SET age = abs(benchmarkdate :: date - birthdate :: date)/ 365;
Any help would be appreciated. Thank you.
I would use EXTRACT here and then take a difference of only the year components on the two dates:
UPDATE animals
SET age = EXTRACT(year FROM benchmarkdate) - EXTRACT(year FROM birthdate);
Note that you might even want to avoid doing this update, and instead just compute the age when you select. If you foresee the need to frequently do such an update, that would be a good indicator that you probably should change your approach.
Related
I have looked through a few posts but either there is too much information or I just don't quite understand what I am looking for.
eg: How to get the first and last date of the current year?
In a table I have sales orders for the last 20 years, all I want is to show the orders from January 1st of three years before until whatever the current date is.
AND ShipDate >= (YEAR(ShipDate)-3)
Which doesn't work.
AND ShipDate >= DATEADD(YEAR,-2,GETDATE())
Which Shows exactly 2 years ago from whatever the current day is.
I want to be able to eventually create reports that show each year in the last three on their own but this is the first step and I am not doing well so far!
I want is to show the orders from January 1st of three years before until whatever the current date is.
The best method in SQL Server is:
where shipdate >= datefromparts(year(getdate()) - 3, 1, 1)
For any date in 2021, this returns all rows from 2021, 2020, 2019, and 2018. If you don't want 2018, then use - 2 rather than - 3.
This formulation has the option of being optimizer friendly (allowing the use of indexes and partitions, for example). Two other common says to write this are not optimizer friendly:
where datediff(year, shipdate, getdate()) <= 3
where year(shipdate) >= year(getdate()) - 3
(Once again, the "3" might be "2" depending on what you really intend.)
you were close:
AND YEAR(ShipDate) >= (YEAR(ShipDate)-3)
You were comparing a year to a date
Currently I have 2 years worth of Sales data, and my company wants a weekday to weekday comparison between the 2 years
basically the Sales Numbers for Wednesday April 1st 2020 should be compared to Wednesday April 3rd 2019 (weekday to weekday comparison, instead of calendar year)
i'm trying to come up with some sort of case statement that will allow me to add a helper column so I can then put it into an excel Pivot table, or use a join, but I'm a bit stumped on how to go about it
Basically I thought if I could come up with some way to assign a sequence to each workday, it could be done
for example Monday #1 in April could get the code MApril1 Monday #2 in April could get MApril2 Monday #1 in June could get MJune1 and so forth (I could just do that for both years and that would make it very easy)
but I just cant think of a way to write that sequence or case, any suggestions?
long story short lets say I gave you the date Wednesday April 8th 2020, how would you identify this as the second Wednesday of April 2020?
Not sure if I understood clearly but I will try to help.
Don't hesitate to tell me if I'm wrong.
I assumed you used SQL Server. Please add the SQL you use into the tags.
I would count the week number.
set datefirst 1; -- depending on the language
declare #weekNb int;
select #weekNb = datepart(week, #yourInitialDate); -- getting the number of the week
declare #dayNb = int;
select #dayNb = datepart(weekday, #yourInitialDate); -- getting the number of the day
And now, using the function from this post I would get the day from the current year :
select dbo.date_from_week_number_day(YEAR(GetDate(),weekNb,dayNb);
I'd like to seek help with my access database. i would like to distribute value (format on currency) into multiple field base on date start and date end.
for example on:
if field Spend Amount value is - $10,000
if field start date is - Jan-2018
if Field end date is - April-2018
then the Jan field will be $2,500
and Feb field should be $2,500
Mar field also $2,500
april field also $2,500
basically the amount should be equally divided base on start month and end month category
appreciate your help please
The expression to put in your update can look something like this:
Amount/(DateDiff("m",StartDate,EndDate)+1)
This will divide 10.000 with 4. The 4 comes from the DateDiff that counts the number of months between the start and end (which results in 3) plus one. Make sure to test edge cases like Nov 30 to Dec 1, leap years and other problematic combinations.
Edit, clarification - this is not VBA. Put it in an update statement
I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;
I've periods of time every 15 days, MOM and EOM.
What I need to check is if a date value on a date field is prior to the current period minus 1.
For example, if today is 12/29, the period is 12/31, and i need to check
if prior < 12/15
How can i get the EOM (End Of Month, i mean, the last day of the month) and the MOM (Middle of month, it's like every 15th of month) with GETDATE() function without doing a DATEADD with -15 days (because in feb will be fail, and i don't care the month)
Any help or work around will be preciated.
Thanks
If you need the value 15 then put it in your code.
If that is against your company's policies then challenge the person that made that policy. Writing 5 lines of code to replace two characters is not a good coding...
If writing the 5 lines made your application much more flexible then maybe I could understand, but you are still "hard coding" 15 into your comparisons.
Thinking in a work around, what I did it was this:
If actual day < 15 then get the month actual, convert to the first day of the month (01) and minus 1 day. I get the last day of the prior month. (EOM - 1 period)
If actual day > 15, then the prior period (MOM - 1 period) is: 15 of actual month.
It's a query with if structure.
If someone has a better answer, please answer it and I'll be accept it.
Thanks :)