Difference in days between two dates in Impala - date

I am trying to find a date difference In Impala. I have tried a few options. my most recent is below
ABS(dayofyear(CAST(firstdate AS TIMESTAMP)-dayofyear(CAST(seconddate AS TIMESTAMP)
an example of data looks like:
firstDate:
2017-11-25
secondDate: 2017-11-30
The solution that I am looking for in the above scenario would be five. The provided code would give me this answer, but the trouble comes in when the first date is in december and the second one ends in January.
Firstdate: 2016-12-30 seconddate:2017-12-30
with the above code this gives me an output of 358 days,the outcome that I would want to see is 8 days. This code is only looking at what day of the year this falls on, and is not able to consider moving from year to year. I know that there is no datediff function on Impala and I've also tried using just subtraction on the two timestamps but that didn't work either. I have scoured google and have come back empty any suggestions would be greatly appreciated

SQL
SELECT DATEDIFF(TO_DATE(firstdate), TO_DATE(seconddate));
Is this what you are looking for?

Related

Acumatica, creating a generic inquiry with a condition on a field equal to the previous day

I have created a simple generic inquiry that shows some data after joining two tables. I am now struggling to implement a condition that should make the result only contain rows where a date field is equal to yesterday date.
I am a bit annoyed as Acumatica has created the method `today()` neatly, but not a `yesterday()` or `tomorrow()`.
Does anyone have a good solution that they care to share?
Help is much appreciated,
Cheers.
P.s
I have made this very hackish solution that somewhat works, but breaks between change in month or year.
=CStr(Year(Today()))+CStr(Month( Today() ))+CStr( Day(Today())-1)
Okay, so I found the solution to my problem!
I read that Acumatica is coded C# and.NET which made me think of how adding dates is done there. You utilise the DateAdd(date, interval, number) under the time tab to create relative changes to date.
In this case, the date is the time source from where you want to change, interval is in which interval/dimension you wanna move, for example, 'd' is for days. You can find more prefixes here: crosspost. Lastly, number is how much you want to change in the interval, in my case -1.
TLDR; Use DateAdd(Today(), 'd', -1) to get the previous date in relation to the current date.
You can simply write Equals to #Today-1 for Yesterday like below:

Recoding dates so overnight hours count as one day

I am working with overnight data (in particular I am concerned about the hours 6PM-6AM). I want this time frame to count as “day 1” for my study. My problem is that the date changes at midnight. Can anyone recommend code to reassign the time-frames that I am concerned about as “day1, day 2, etc.” instead of the Julian date. I am new to this so any suggestions would be very much appreciated!
*Update, here is a screenshot of the data I am working with. So, the column on the left is the julian date.. in this example I am looking at day 305 and 306 and the hours 18:20 (305) to 6:20 (306). I want these hours to be classified as day 1, instead of the date changing at midnight. Also, I need to get rid of the random date that is attached to my time column. I'm sure there is an easy way to do that, but if anyone knows off the top of your head I'd appreciate that!
Again, I'm very new to R and coding language, please be gentle!
Thanks a million.
You didn't mention what language you're using.
Given timespans 6pm to 6am, I believe the simplest solution would be to decrement 7 hours from any given date - then all of your timestamps would have the start day of the timespan as their day component.

Difference in Days in DB2

I have to calculate the difference in days between two dates and I figured out that
there is no such thing as a DATEDIFF() function in DB2.
I tried doing it like that:
(DAYOFYEAR(date1)-DAYOFYEAR(date2)+(YEAR(date1)-YEAR(date2))*365)
This is obviously not working for leap years, but I do not have to deal with that.
I know that date1 is always later than date2.
Do I have any flaws in my logic? It is not working (it's an exercise and I have a function to test my results). Is there an easier way to do that?
Thank you.
It would possibly help if we knew what version of DB2 you were using and what platform it was running on. But it seems likely that you can do something like this:
select
days(my1stdate) - days(my2nddate) as myDuration
from mySchema.myTable
The DAYS() function converts a DATE value into the number of days between Jan 1, 0001, and the supplied DATE value. Once both DATEs are converted, the subtraction (difference) is straightforward.

Grouping Expert, Current date against start date

I am having trouble grouping certain results in a work in progress report that arranges by start date, I have grouped using fixed values before but because the dates keep moving I am unsure what to do.
The start date is WIP_Schedule.Start_Date
the groups I am trying to create are:
[Group1] Overdue = the current date has passed the start date.
[Group2] (Yet to be named) = the current date 2 week period prior to the start date
[Group3] To Do = the current date after the two week period prior to the start date.
I am after a works instruction on how to achieve this.
I know this isn't a lot of information, if you require any more please ask.
Thanks,
Daniel
This is a pretty straightforward requirement, so you should be able to figure it out by searching the web. However, I'll give you part of the answer and hopefully you can figure it out.
Start by creating a formula to figure out the status of the date.
If {WIP_Schedule.Start_Date} > current date then "Overdue"
Else......
Then you can group based on that formula. All you have to do is figure out the rest of the formula.

Calculating days between last login and current date

Upon logging into their accounts, each user has their login date and time stored to the database. What I was looking to do however is figure out the amount of days (or preferably convert into months if greater than a month) so that if a user views their profile they can see how active the band are. Also, this could benefit me in terms of keeping active profiles top of the agenda for content on the site so that it doesn't become stale from inactive users content filling up main page content.
I'm using ColdFusion so i'd be looking for a way to find for example how many days ago #lastLogin# was from #now()#. So say if the date of the last login was 23/04/2013 and todays date is 29/04/2013 it would read "Last Active, 1 day ago." However if the last login was 23/03/2013, it would read "Last Active, 1 month ago".
Anybody know how to do this? Thanks.
P.S I currently have no code from testing this as I have no idea where to start in terms of achieving this.
Use DateDiff
<cfset days = dateDiff("d", LoginDateVariable, now()) />
It's as simple as that.
P.S I currently have no code from testing this as I have no idea where
to start in terms of achieving this.
This doesn't answer your direct question but to help you know where to get started, I would strongly suggest reviewing the built in ColdFusion functions and tags that are available to you.
Tags
Tags by function
Functions
Functions by category
Also, Google searches usually land you at the docs, just add "coldfusion" to your search string. Searching google for coldfusion date functions yields very helpful answers, the first of which are a list of all ColdFusion date functions.
Dale's answer is spot on. But I would also suggest returning it as a variable with your query. Let the SQL server do the work. It's very efficient for those types of calculations. Not that CF can't do them well, too. But it's probably more appropriate for SQL to do that lifting. Especially if you're already returning the lastLogin date.
It would be similar to the CF solution:
SELECT ...., lastLogin, DATEDIFF(d, lastLogin, GETDATE()) AS LastLoginDays
FROM ....
WHERE ....
That would give you the number of days. You'd have to decide how you wanted to define a month if you wanted to break it out by month/day. That would get a bit more complex. You could write a SQL function that could be run on both dates and give you an accurate count of days/months/years since last login.
One other thing to keep in mind: Where are the dates being generated? When you insert loginDate into the database, are you doing a now() in CF before you insert it or are you doing a getDate() in SQL when you insert it? Again, I would let the database do your date logic, but you'd want to compare the two dates from the same source. For instance, if your loginDate was a database getDate() then you may not want to compare that to a CF now(). One goes by the datetime of the SQL server and the other goes by the datetime of the CF server. They could be different.