I need to get the difference between dates, but I just need to get the whole months that have passed. So for example between "1990-05-24" and "1990-05-27" it should say 0. It would also be 0 for "1990-05-02" and "1990-05-29" because the month has not finished.
I already got the difference in months using MONTHS_BETWEEN(), but I get months with decimals, and ROUNDing is not an option since sometimes it should be up and sometimes down.
I thought about setting al dates to day 01. In both colums Closing_date and Opening_date. But can't figure out how to do it.
I think you want to count boundaries between months. If so, you can use months_between() after truncating to the first of the month:
months_between(trunc(date1, 'MON'), trunc(date2, 'MON')
Related
I know Quicksight could help parse the time into DAY, WEEK, MONTH and YEAR in Field Wells, but how could I customize it into 3 days(72 hrs)? Is there any windowsFunction I could use?
You can introduce a calculated field with formula looking something like that:
addDateTime((extract("DD", {create_time})/3)*3, "DD", truncDate("DD", {create_time}))
And use it for grouping. Depends on your desired time interval it possibly will not do exactly what you need - cause it will group days in month by three, but you can use dateDiff and either some fixed date as starting point. For example:
addDateTime((dateDiff(parseDate("2020-01-01"), truncDate("DD", {create_time}))/3)*3, "DD", parseDate("2020-01-01"))
here like these queries,
I.e: here is First date of year is calculated ,
select DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
and here 0 are used,what is the explanation for that?
You mean "Why", obviously.
0 is an arbitrary date which is easy to write, I think 1st January 1753. It does not matter in the all-too-common dateadd(datediff()) formula, ANY date/time would be just as good if put in both the dateadd's and the datediff's arguments. But, 0 is easy to write, plus everyone is doing the same and it creates some comfort seeing it and understanding it.
The way dateadd-datediff works is this: eg in your query
DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
, you want the first moment of the year of getdate(). How do you do this? You ask how many years have passed, from 1st January 1753, to getdate(3 July 2018), which results in 265. You then add that number(265) of years to 1st January 1753. Because you only added the years, and not months, days, hours etc you will get 1/1/2018 00:00:00.
I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12
I found a place in our old code where the original programmer tried to calculate whether an employee had been hired for a certain number of years. The calculation used the difference in days between the date hired and today divided by 364. This didn't make sense to me so I changed it to the difference in years. This also seemed to give an incorrect answer. Does DateDiff round up to the nearest year? Running this formula in the immediate window gives 15 as the answer. I was hoping it would give 14.
?datediff("yyyy",#3/1/1999#,#2/19/2014#)
Would it be better to use.
?datediff("m",#3/1/1999#,#2/19/2014#)/12
DateDiff for years only considers the year parts of the dates you supply. And it does not return what you might want as "how many years" ...
For example, the last day of 2013 to the first day of 2014 would be one year as far as DateDiff("yyyy" is concerned.
? DateDiff("yyyy", #2013-12-31#, #2014-1-1#)
1
DateDiff rounds off to the very next year if the year difference is like x years and y months.
For example:
if a person's age is 18 years and 1 months, datediff(yy,DDOB,GetDate()) will give result as '19'.
In case you dont want this rounding off, you can
Get difference in days between two dates after casting them in
INTEGER
Divide the difference with 365.25
Use FLOOR to ignore the the decimal part (don't round off as the
next number):
FLOOR((CAST (GetDate() AS INTEGER) - CASR(YourDate AS INTEGER)) / 356.25)
You can do the combination of IIf,DateDiff and DateAdd, like this:
=IIf(DateDiff("m";DateAdd("yyyy";DateDiff("yyyy";[DDOB];Date());[DDOB]);Date())<0;
DateDiff("yyyy";[DDOB];Date())-1;
DateDiff("yyyy";[DDOB];Date()))
So, firstly you calculate months between DDOB plus DateDiff years and Date(), and if integer form DateDiff "m" are in minus, then IIf will reduce the value for the year for one.
I am writing a little query in SQL and am butting heads with an issue that it seems like someone must have run into before. I am trying to find the number of months between two dates. I am using an expression like ...
DATEDIFF(m,{firstdate},{seconddate})
However I notice that this function is tallying the times the date crosses the monthly threshold. In example...
DATEDIFF(m,3/31/2011,4/1/2011) will yield 1
DATEDIFF(m,4/1/2011,4/30/2011) will yield 0
DATEDIFF(m,3/1/2011,4/30/2011) will yield 1
Does anyone know how to find the months between two dates more-so based upon time passed then times passed the monthly threshold?
If you want to find some notional number of months, why not find the difference in days, then divide by 30 (cast to FLOAT as required). Or 30.5-ish perhaps - depends on how you want to handle the variable month length throughout the year. But perhaps that's not a factor in your particular case.
The following statements have the same startdate and the same endate. Those dates are adjacent and differ in time by .0000001 second. The difference between the startdate and endate in each statement crosses one calendar or time boundary of its datepart. Each statement returns 1. ...
SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000'); ....
(from DATEDIFF, section datepart Boundaries ). If you are not satisfied by it, you probably need to use days as unit as proposed by martin clayton
DATEDIFF(m,{firstdate},ISNULL({seconddate},GETDATE())) - CASE
WHEN DATEPART(d,{firstdate}) >= DATEPART(d,ISNULL({seconddate},GETDATE()))
THEN 1
ELSE 0
DATEDIFF is like this by design. When evaluating a particular time measurement (like months, or days, etc.), it considers only that measurement and higher values -- ignoring smaller ones. You'll run into this behavior with any time measurement. For example, if you used DATEDIFF to calculate days, and had one date a few seconds before midnight, and another date a few seconds after midnight, you'd get a "1" day difference, even though the two dates were only a few seconds apart.
DATEDIFF is meant to give a rough answer to questions, like this:
Question: how many years old are you?
Answer: some integer. You don't say "I'm 59 years, 4 months, 17 days, 5 hours, 35 minutes and 27 seconds old". You just say "I'm 59 years old". That's DATEDIFF's approach too.
If you want an answer that's tailored to some contextual meaning (like your son who says "I'm not 8! I'm 8 and 3-quarters!, or I'm almost 9!), then you should look at the next-smallest measurement and approximate with it. So if it's months you're after, then do a DATEDIFF on days or hours instead, and try to approximate months however it seems most relevant to your situation (maybe you want answers like 1-1/2 months, or 1.2 months, etc.) using CASE / IF-THEN kinds of logic.