Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract two dates in SQL and get days of the result

Select I.Fee
From Item I
WHERE GETDATE() - I.DateCreated < 365 days

How can I subtract two days? Result should be days. Ex: 365 days. 500 days.. etc...

like image 603
Cute Bear Avatar asked Jan 02 '13 09:01

Cute Bear


People also ask

Can you subtract 2 dates in SQL?

The DATEDIFF() function returns the difference between two dates.

How do you subtract working days in SQL?

If you only care about weekends and ignore holidays, you can use the following formula: DATEADD(DAY, -7*(@bdays / 5) + @bdays % 5, @start_date) . Then if the result falls on Saturday subtract 1 day, if it falls on Sunday subtract 2 days.

How do I subtract a day from a date in SQL?

Following the answer from Philip Rego, you can use SELECT GETDATE() - 1 to subtract days from a date.


6 Answers

Use DATEDIFF

Select I.Fee
From Item I
WHERE  DATEDIFF(day, GETDATE(), I.DateCreated) < 365
like image 84
Habib Avatar answered Oct 05 '22 16:10

Habib


use DATE_DIFF

Select I.Fee
From   Item I
WHERE  DATEDIFF(day, GETDATE(), I.DateCreated)  < 365
  • DATE_DIFF
like image 30
John Woo Avatar answered Oct 05 '22 15:10

John Woo


EDIT: It seems I was wrong about the performance on the code example. The best performer is whichever snippet runs second in the posted case. This demonstrates what I was trying to explain, and the time differences are not as dramatic:

----------------------------------
--  Monitor time differences
----------------------------------
CREATE CLUSTERED INDEX dtIDX ON #ArbDates (MyDate)
DECLARE @Stopwatch DATETIME 
SET @Stopwatch = GETDATE()
    -- SARGABLE
    SELECT *
    FROM #ArbDates
    WHERE MyDate > DATEADD(DAY, -364, '2010-01-01')


PRINT DATEDIFF(MS, @Stopwatch, GETDATE())
SET @Stopwatch = GETDATE()
    -- NOT SARGABLE
    SELECT *
    FROM #ArbDates
    WHERE DATEDIFF(DAY, MyDate, '2010-01-01') < 365
PRINT DATEDIFF(MS, @Stopwatch, GETDATE())

Excuse me for posting late and my crudely commented example, but I think it important to mention SARG.

SELECT I.Fee
FROM Item I
WHERE  I.DateCreated > DATEADD(DAY, -364, GETDATE())

Although the temp table in the code below has no index, the performance is still enhanced by the fact that a comparison is done between an expression and a value in the table and not an expression that modifies the value in the table and a constant. Hope this is found to be useful.

USE tempdb
GO

IF OBJECT_ID('tempdb.dbo.#ArbDates') IS NOT NULL DROP TABLE #ArbDates
DECLARE @Stopwatch DATETIME 

----------------------------------
--  Build test data: 100000 rows
----------------------------------
;WITH Base10 (n) AS
(
    SELECT 1 UNION ALL  SELECT 1 UNION ALL  SELECT 1 UNION ALL
    SELECT 1 UNION ALL  SELECT 1 UNION ALL  SELECT 1 UNION ALL
    SELECT 1 UNION ALL  SELECT 1 UNION ALL  SELECT 1 UNION ALL
    SELECT 1
)
,Base100000 (n) AS
(
    SELECT 1
    FROM Base10 T1, Base10 T3, Base10 T4, Base10 T5, Base10 T6
)
SELECT MyDate = CAST(RAND(CHECKSUM(NEWID()))*3653.0+36524.0 AS DATETIME) 
INTO #ArbDates 
FROM Base100000

----------------------------------
--  Monitor time differences
----------------------------------
SET @Stopwatch = GETDATE()

    -- NOT SARGABLE
    SELECT *
    FROM #ArbDates
    WHERE DATEDIFF(DAY, MyDate, '2010-01-01') < 365

PRINT DATEDIFF(MS, @Stopwatch, GETDATE())
SET @Stopwatch = GETDATE()

    -- SARGABLE
    SELECT *
    FROM #ArbDates
    WHERE MyDate > DATEADD(DAY, -364, '2010-01-01')

PRINT DATEDIFF(MS, @Stopwatch, GETDATE())
like image 38
MarkD Avatar answered Oct 05 '22 15:10

MarkD


SELECT DATEDIFF(day,'2014-06-05','2014-08-05') AS DiffDate

diffdate is column name.

result:

DiffDate

23

like image 28
aykut aydoğan Avatar answered Oct 05 '22 17:10

aykut aydoğan


How about

Select I.Fee
From Item I
WHERE  (days(GETDATE()) - days(I.DateCreated) < 365)
like image 22
Rick Avatar answered Oct 05 '22 16:10

Rick


SELECT (to_date('02-JAN-2013') - to_date('02-JAN-2012')) days_between
FROM dual
/
like image 33
Art Avatar answered Oct 05 '22 17:10

Art