Datediff function in Access Database - sql

I am trying to use Datediff function while subtracting two dates. One date is with Date and time stamp and the other with date only. How to get the difference of dates?
Here Column1 is 7/11/2017 4:24:38 PM and Column2 is 15/12/2017 where there is no timestamp.
DateDiff("d",[Column1],[Column2])

Convert the date column into datetime using Format function. See example below.
EDIT: since you want the difference in days and decimal point, I get the difference in hours then divide by 24. You can be as accurate if you want by getting the difference in minutes or seconds but using a different divisor.
SELECT DateDiff("h",
Now(),
Format('04/05/2018','mm/dd/yyyy hh:nn:ss'))/24 AS Expr1;
result: 1.125 days

Related

How to calculate exact hours between two datetime fields?

I need to calculate hours between datetime fields and I can achieve it by simply doing
select date1,date2,(date1-date2) from table; --This gives answer in DD:HH:MM:SS format
select date1,date2,(trunc(date1)-trunc(date2))*24 --This doesn't take into account the time, it only gives hours between two dates.
Is there a way I can find the difference between date times that gives the output in Hours as a number?
The 'format' comment on your first query suggests your columns are timestamps, despite the dummy column names, as the result of subtracting two timestamps is an interval. Your second query is implicitly converting both timestamps to dates before subtracting them to get an answer as a number of days - which would be fractional if you weren't truncating them and thus losing the time portion.
You can extract the number of hours from the interval difference, and also 24 * the number of days if you expect it to exceed a day:
extract(day from (date1 - date2)) * 24 + extract(hour from (date1 - date2))
If you want to include fractional hours then you can extract and manipulate the minutes and seconds too.
You can also explicitly convert to dates, and truncate or floor after manipulation:
floor((cast(date1 as date) - cast(date2 as date)) * 24)
db<>fiddle demo
Use the DATEDIFF function in sql.
Example:
SELECT DATEDIFF(HOUR, '2021-09-05 12:00:00', GETDATE());
You can find it using the differnece of dates and multiplying with 24
select date1
,date2
,(date1-date2)*24 as diff_in_hrs
from table

SQL Solr query Convert date to

I'm interested in the question: how to convert date to number in millis with Solr SQL? Is it possible?
You have to use Function Queries (https://lucene.apache.org/solr/guide/6_6/function-queries.html)
For example: in the field returned by your query, just insert ms(2000-01-01T00:00:00Z) or ms(mydatefield)
http://localhost:8983/solr/job/select?fl=ms(2000-01-01T00:00:00Z)&indent=on&q=:&wt=json
result: 946684800000
Obs2: Dates are relative to midnight, January 1, 1970 UTC (you can use function queries and calculate milliseconds between to dates)
Obs1: your date field type (mydatefield in the above example) should be a TrieDateField

SQL : How sysdate minus a value works?

I found below code in my existing project.
select * from mytable where SomeColumn_date >= trunc(sysdate)-.25/24;
Sample value for SomeColumn_date is 22-JUN-17 05:46:55
How does SomeColumn_date >= trunc(sysdate)-.25/24 work on Date data type?
Different database engines allow different operations to be applied to date data types. For the most part, an operation of <Date Value> +/- 1 will add or subtract one day to that date value. This is syntactically equivalent to the dateadd function when using days.
In your example here, the -.25/24 resolves to the number equivalent of -15 minutes, which is then subtracted from your date value.
It is essentially a much less readable version of datedd(min,-15,<Date Value>).
From the documentation of TRUNC (I'm guessing you are using Oracle):
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. [...] If you omit fmt, then date is truncated to the nearest day.
The result of trunc(sysdate) would be the present date without the time component. Now .25/24 (actually meaning 0.25/24) is substracted from that. If you substract a date using - the operand is always in days. 0.25/24 would be a form to express a quarter of an hour.
So trunc(sysdate)-.25/24 would result in yesterday 23:45.
Ok so 2 things are happening here:
trunk(date,fmt)
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. If you omit fmt, then date is truncated to the nearest day.
So if you have suppose 22-JUN-17 05:46:55 you get 22-JUN-17. Since you don't have the fmt
DATETIME - .25/24 implies .25 hours before your current Date time.
But since you have only DATE all it does is .25 hours before todays 12:00 AM i.e yesterdays 11:45PM
SomeColumn_date >= trunc(sysdate)-.25/24
So suppose if its 22-JUN-2017 right now the date is compared to 21-JUN-2017 11:45 PM
NOTE: - is for before current time, + is for after the current time

difference between two timestamps (in days) in oracle

SELECT MIN (snap_id) AS FIRST_SNAP,
MAX (snap_id) AS LAST_SNAP,
MIN (BEGIN_INTERVAL_TIME) AS FIRST_QUERY,
MAX (END_INTERVAL_TIME) AS LAST_QUERY,
max(end_interval_time) - min(begin_interval_time) as "TIME_ELAPSED"
FROM dba_hist_snapshot
ORDER BY snap_id;
2931 3103 5/28/2012 6:00:11.065 AM 6/4/2012 11:00:40.967 AM +07 05:00:29.902000
I would like the last columns output to be 7 (for the days). I have tried trunc and extract like some other posts mentioned but can't seem to get the syntax right. Any ideas?
Judging from your comment, you're using timestamp columns, not datetime. You could use extract to retrieve the hour difference, and then trunc(.../24) to get the whole number of days:
trunc(extract(hour from max(end_interval_time) - min(begin_interval_time))/24)
Or you could cast the timestamp to a date:
trunc(cast(max(end_interval_time) as date) -
cast(min(begin_interval_time) as date))

How to get Date diffrence in hh:mm format In Select Query Sql 2005

I am trying to get the the result of in time and out time from dates
but it returns only hours using following select Query as follows
SELECT DATEDIFF(Hh,InTime,OutTime) as Diff_time from EmpLogTable
and i need result in HH:MM
Suppose my in time is 11 am and out is 5.49pm so o/p would be 6.49 but
using above select query i am getting o/p as 7 only
if any body has a solution then please let me know
Thanking you in Advance
Umesh Rakhe
The DATEDIFF function returns an INT so it will not work as you like, your best bet is to subtract InTime from OutTime or use DATEDIFF with minutes (n) instead.
you should probably do UI formatting on the client, not the database
you can use diff = datediff(mi, intime, outtime) to get the difference in minutes
then divide diff by 60 to get the hours
and take the modulus diff % 60 to get the remaining minutes
then turn into strings and you're good to go
SELECT Outime - InTime as Diff_time from EmpLogTable
DATEDIFF measures day, hour etc boundaries: you're asking for a true date/time difference. In this case, I'd simply subtract the values rather than using a complex nested DATEDIFF. Or do it in the client.
If you have a interval > 24 hours though, then you'd need a nested DATEDIFF to get 25 hours: my answer would give one day and one hour.
The hh:nn:ss format can be done via CONVERT with style 108, but again I'd do this in the client