datediff parameters in sql server code example
Example 1: date difference in number of days sql server
DECLARE
@start_dt DATETIME2= '2019-12-31 23:59:59.9999999',
@end_dt DATETIME2= '2020-01-01 00:00:00.0000000';
SELECT
DATEDIFF(year, @start_dt, @end_dt) diff_in_year,
DATEDIFF(quarter, @start_dt, @end_dt) diff_in_quarter,
DATEDIFF(month, @start_dt, @end_dt) diff_in_month,
DATEDIFF(dayofyear, @start_dt, @end_dt) diff_in_dayofyear,
DATEDIFF(day, @start_dt, @end_dt) diff_in_day,
DATEDIFF(week, @start_dt, @end_dt) diff_in_week,
DATEDIFF(hour, @start_dt, @end_dt) diff_in_hour,
DATEDIFF(minute, @start_dt, @end_dt) diff_in_minute,
DATEDIFF(second, @start_dt, @end_dt) diff_in_second,
DATEDIFF(millisecond, @start_dt, @end_dt) diff_in_millisecond;
Example 2: how to get the date diff on once field in sql server
SELECT t1.OrderNo,DATEDIFF(day,t1.LoadedStartDate,t2.LoadedStartDate)
FROM UnnamedTableFromQuestion t1
INNER JOIN
UnnamedTableFromQuestion t2
on
t1.OrderNo = t2.OrderNo
WHERE t1.OpNo = 1 and
t2.OpNo = 4
select cur.unique_id_field, cur.seq_no, cur.date_created ,
datediff(second, prv.date_created, cur.date_created) as diff_in_seconds
from yourtable as cur
join yourtable as prv
on cur.seq_no = prv.seq_no + 1;