Date-difference will give difference in the number of days, months, years etc. between any two calendar dates. Not defined by ISO/ANSI SQL, but several vendors have similar implementations.
Questions tagged [datediff]
1599 questions
8
votes
6 answers
Difference with microseconds precision between two DateTime in PHP
I can get a datetime with microseconds in PHP with a workaround as:
list($usec, $sec) = explode(" ", microtime());
echo date("Y-m-d\TH:i:s", $sec) . "." . floatval($usec)*pow(10,6);
I need the difference with microseconds between two datetimes,…

Jose Nobile
- 3,243
- 4
- 23
- 30
8
votes
7 answers
Date Difference in MySQL to calculate age
I have a problem regarding the datediff MYSQL function, I can use it and it is simple. But I don't understand how to use it to collect differences within the table field. E.g.
I have a column dob and I want to write a query that will do something…

chsab420
- 191
- 2
- 3
- 14
7
votes
4 answers
Working days between two dates in Snowflake
Is there any ways to calculate working days between two dates in snowflake without creating calendar table, only using "datediff" function

Jay Tilala
- 141
- 1
- 1
- 9
7
votes
2 answers
Date Difference Between Two Device Failures
I am trying to calculate the # of days between failures. I'd like to know on each day in the series the # of days passed since the last failure where failure = 1. There may be anywhere from 1 to 1500 devices.
For Example, Id like my dataframe to…

Starbucks
- 1,448
- 3
- 21
- 49
7
votes
1 answer
How to get datediff() in seconds in pyspark?
I have tried the code as in (this_post) and cannot get the date difference in seconds. I just take the datediff() between the columns 'Attributes_Timestamp_fix' and 'lagged_date' below.
Any hints?
Below my code and output.
eg =…

a_geo
- 157
- 1
- 1
- 6
7
votes
5 answers
Undefined date_diff()
I'm trying to use date_diff():
$datetime1 = date_create('19.03.2010');
$datetime2 = date_create('22.04.2010');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
Its doesn't work for me, gives an error:
Call to…

James
- 42,081
- 53
- 136
- 161
7
votes
4 answers
Find if date is more than another date
I can't work out why this VB.net code is not working..
What I am trying to do is if value1 > value2 then show a messagebox saying expired else show a messagebox saying not expired.
If "4-3-13 10:54:22" > "15-3-13 12:23:30" Then
…

Aaron
- 3,389
- 12
- 35
- 48
7
votes
5 answers
Count the number of rows in 30 day bins
Each row in my table has a date time stamp, and I wish to query the database from now, to count how many rows are in the last 30 days, the 30 days before that and so on. Until there is a 30 day bin going back to the start of the table.
I have…

seanieb
- 1,196
- 2
- 14
- 36
7
votes
4 answers
SQL DateDiff without enddate
I'm using SQL Server
I need to know the number of days that a patient was receiving a treatment.
The problem is that I can only get a startDate but not an endDate
When I run a query and order it by StartDate I get something like…

loperam
- 73
- 2
7
votes
2 answers
Difference between two date& time in datetime Fields in SQL
Possible Duplicate:
How to compare two dates to find time difference in SQL Server 2005, date manipulation
I had two datetime fields and should calculate the between them including time fields.
If the difference between them is less than 24 hours…

sateesh
- 87
- 1
- 1
- 8
6
votes
5 answers
Using DATEDIFF in T-SQL
I am using DATEDIFF in an SQL statement. I am selecting it, and I need to use it in WHERE clause as well. This statement does not work...
SELECT DATEDIFF(ss, BegTime, EndTime) AS InitialSave
FROM MyTable
WHERE InitialSave <= 10
It gives the…

Josh Stodola
- 81,538
- 47
- 180
- 227
6
votes
1 answer
EFCore 2.2 GroupBy Sum and DateDiff
I'm trying to translate the following SQL in to an EF Core query and I'm getting warnings that GroupBy and Sum will be evaluated locally. Is there anyway currently to write this that it will fully translate to SQL?
SELECT UserId, ST.StatusId,…

lanky393
- 292
- 4
- 12
6
votes
1 answer
Deleting records having a date older than 3 days, for rolling 3 day job?
Prior to executing a sensitive sql command I wanted to do a sanity check.
I am trying to delete records that have a [LoadDt] date value older than 3 days and my code is:
delete IntraDayStats
where DATEDIFF(dd, LoadDt, dateadd(d,-3, getdate()) ) >= 3…

kacalapy
- 9,806
- 20
- 74
- 119
6
votes
4 answers
Calculate Time Difference Between Two Consecutive Rows
I have a table like this:
RecordID TransDate
1 05-Oct-16 9:33:32 AM
2 05-Oct-16 9:33:37 AM
3 05-Oct-16 9:33:41 AM
4 05-Oct-16 9:33:46 AM
5 05-Oct-16 9:33:46 AM
I need to get the difference…

controller
- 185
- 1
- 2
- 11
6
votes
3 answers
Average time between dates in same field by groups
I have a transactional database with sales data and user id like the following:
id_usuarioweb dt_fechaventa
1551415 2015-08-01 14:57:21.737
1551415 2015-08-06 15:34:21.920
6958538 2015-07-30 09:26:24.427
6958538 …

eflores89
- 339
- 2
- 10
- 27