Need date: Today - 2 years..

  • 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 !

    BT
  • 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.

    ______________________________________________________________________________________________
    Forum posting etiquette.[/url] Get your answers faster.

  • 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


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • thank you.. now, how might I modify the above solution:

    SELECT DATEADD(YEAR,-2,CURRENT_TIMESTAMP)

    to render a TIME = all 0's

    BT
  • 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


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • 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))

    ______________________________________________________________________________________________
    Forum posting etiquette.[/url] Get your answers faster.

  • 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.

    ================================================================================


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Hi,

    If i am not wrong, you are looking for this:-

    SELECT DATEPART("YYYY",GETDATE())-2

    regards

    Palash Gorai

  • 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.

    ______________________________________________________________________________________________
    Forum posting etiquette.[/url] Get your answers faster.

  • 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.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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