September 27, 2011 at 5:51 am
This statement renders 2 years ago today:
SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()) -730, 0)
How can I modify the above statement to use the YEAR instead of dd -- to obtain same result?
(eg... YEAR -2)
thanks for any help !
September 27, 2011 at 5:58 am
Express12 (9/27/2011)
This statement renders 2 years ago today:SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()) -730, 0)
How can I modify the above statement to use the YEAR instead of dd -- to obtain same result?
(eg... YEAR -2)
thanks for any help !
You've already got it.
DATEADD(YEAR,-2,CURRENT_TIMESTAMP)
check out BOL for more date modifications and conversions.
September 27, 2011 at 6:03 am
Express12 (9/27/2011)
This statement renders 2 years ago today:SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()) -730, 0)
How can I modify the above statement to use the YEAR instead of dd -- to obtain same result?
(eg... YEAR -2)
thanks for any help !
That's not what your statement is doing. . . only the -730 (365/2) is to do with changing the date. The rest of that statement is setting the time to 00:00:00
September 27, 2011 at 6:05 am
thank you.. now, how might I modify the above solution:
SELECT DATEADD(YEAR,-2,CURRENT_TIMESTAMP)
to render a TIME = all 0's
September 27, 2011 at 6:07 am
Express12 (9/27/2011)
thank you.. now, how might I modify the above solution:SELECT DATEADD(YEAR,-2,CURRENT_TIMESTAMP)
to render a TIME = all 0's
SELECT DATEADD(dd, DATEDIFF(dd,0,DATEADD(YEAR,-2,GETDATE())), 0)
--Equivalent
SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()) -730, 0)
But it's really no better than your previous code
September 27, 2011 at 6:09 am
Have you checked that link? There's all the date conversions and date manipulations you'll ever need there.
DATEADD(YEAR,-2,cast(CURRENT_TIMESTAMP as DATE))
September 27, 2011 at 6:19 am
calvo (9/27/2011)
Have you checked that link? There's all the date conversions and date manipulations you'll ever need there.
DATEADD(YEAR,-2,cast(CURRENT_TIMESTAMP as DATE))
Just to prove that I do listen when I'm taught something (message to Jeff Moden and Drew Allen who corrected my misconception in March)
/**********************************************************************************************************************
Purpose:
Create a voluminous test table with highly randomized DATETIME data.
--Jeff Moden
**********************************************************************************************************************/
--===== Do this test in a nice safe place that everyone has.
USE TempDB
;
--===== Conditionally drop the test table to make reruns easier
IF OBJECT_ID('dbo.JBMTest','U') IS NOT NULL
DROP TABLE dbo.JBMTest
;
--===== Create and populate a 1,000,000 row test table.
-- "SomeID" has a range of 1 to 1,000,000 unique numbers
-- "SomeDateTimeTime" has a range of >=01/01/2000 and <01/01/2020 non-unique date/times
SELECT TOP 1000000
SomeID = IDENTITY(INT,1,1),
SomeDateTime = RAND(CHECKSUM(NEWID())) * DATEDIFF(dd,'2000','2020') + CAST('2000' AS DATETIME)
INTO dbo.JBMTest
FROM sys.all_columns ac1
CROSS JOIN sys.all_columns ac2
;
--===== This will take the display out of the picture so we
-- can measure the true processing time in memory.
DECLARE @BitBucketDATETIME DATETIME
PRINT '========== BASELINE =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = SomeDateTime
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
PRINT '========== DATEADD/DATEDIFF 1 =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = DATEADD(dd, DATEDIFF(dd,0,DATEADD(YEAR,-2,SomeDateTime)), 0)
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
PRINT '========== DATEADD/DATEDIFF 2 =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = DATEADD(d, DATEDIFF(d,0,DATEADD(YEAR,-2,SomeDateTime)), 0)
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
PRINT '========== DATEADD/CAST =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = DATEADD(YEAR,-2,cast(SomeDateTime as DATE))
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
PRINT '========== DATEADD/DATEDIFF/-730 =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = DATEADD(dd, DATEDIFF(dd,0,SomeDateTime) -730, 0)
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
PRINT '========== DATEPART/DATEADD/DATEDIFF =========='
SET STATISTICS TIME ON
SELECT @BitBucketDATETIME = DATEPART(YYYY,DATEADD(dd, DATEDIFF(dd,0,SomeDateTime), 0))-2
FROM dbo.JBMTest WITH (NOLOCK)
SET STATISTICS TIME OFF
PRINT REPLICATE('=',80)
========== BASELINE ==========
SQL Server Execution Times:
CPU time = 218 ms, elapsed time = 231 ms.
================================================================================
========== DATEADD/DATEDIFF 1 ==========
SQL Server Execution Times:
CPU time = 452 ms, elapsed time = 464 ms.
================================================================================
========== DATEADD/DATEDIFF 2 ==========
SQL Server Execution Times:
CPU time = 468 ms, elapsed time = 465 ms.
================================================================================
========== DATEADD/CAST ==========
SQL Server Execution Times:
CPU time = 593 ms, elapsed time = 592 ms.
================================================================================
========== DATEADD/DATEDIFF/-730 ==========
SQL Server Execution Times:
CPU time = 327 ms, elapsed time = 339 ms.
================================================================================
========== DATEPART/DATEADD/DATEDIFF ==========
SQL Server Execution Times:
CPU time = 390 ms, elapsed time = 389 ms.
================================================================================
September 27, 2011 at 6:28 am
Hi,
If i am not wrong, you are looking for this:-
SELECT DATEPART("YYYY",GETDATE())-2
regards
Palash Gorai
September 27, 2011 at 6:33 am
Cadavre (9/27/2011)
calvo (9/27/2011)
Have you checked that link? There's all the date conversions and date manipulations you'll ever need there.
DATEADD(YEAR,-2,cast(CURRENT_TIMESTAMP as DATE))
Just to prove that I do listen when I'm taught something (message to Jeff Moden and Drew Allen who corrected my misconception in March)
...
As you can see, the DATEADD/CAST method is slower.
Oh, was this a performance issue that the OP was having? I thought it was just something he hadn't worked with yet.
September 27, 2011 at 6:38 am
calvo (9/27/2011)
Cadavre (9/27/2011)
calvo (9/27/2011)
Have you checked that link? There's all the date conversions and date manipulations you'll ever need there.
DATEADD(YEAR,-2,cast(CURRENT_TIMESTAMP as DATE))
Just to prove that I do listen when I'm taught something (message to Jeff Moden and Drew Allen who corrected my misconception in March)
...
As you can see, the DATEADD/CAST method is slower.
Oh, was this a performance issue that the OP was having? I thought it was just something he hadn't worked with yet.
Never a bad thing to show that one query is comparatively faster than another, it was in a thread exactly like this where I suggested a similar method to the one you posted and was shown a better way.
October 2, 2011 at 12:31 pm
calvo (9/27/2011)
Cadavre (9/27/2011)
calvo (9/27/2011)
Have you checked that link? There's all the date conversions and date manipulations you'll ever need there.
DATEADD(YEAR,-2,cast(CURRENT_TIMESTAMP as DATE))
Just to prove that I do listen when I'm taught something (message to Jeff Moden and Drew Allen who corrected my misconception in March)
...
As you can see, the DATEADD/CAST method is slower.
Oh, was this a performance issue that the OP was having? I thought it was just something he hadn't worked with yet.
From what the OP wrote... nope. But performance should always be a part of the issue taking second only to getting the right answer. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
October 2, 2011 at 12:51 pm
The only problem I see with subtracting 730 days for 2 years, it isn't always 2 years. You have to account for leap year at times or you may not get what you expect.
Bottom line is to test, test, and test again.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply