Calculate and return the previous Date at 18:00 Hours

  • How can I calculate and return the previous Date at 18:00 Hours?

    Here is a miserable attempt:

    DECLARE @RunDate SmallDateTime

    DECLARE @CurrentDate SmallDateTime

    DECLARE @RunDateWoTime SmallDateTime

    SET @CurrentDate = GETDATE()

    SET @RunDate = DATEADD(day,-1,@CurrentDate)-- AS CurrentDate

    SELECT @RunDate AS RunDate

    -- Desired Result is the following:

    -- 2014-03-03 18:00

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • SELECT DATEDIFF(dd,0,GETDATE())-1

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)

    SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))

    Use q3. q1 and q2 help explain the algorithm.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Welsh Corgi (3/4/2014)


    How can I calculate and return the previous Date at 18:00 Hours?

    Here is a miserable attempt:

    DECLARE @RunDate SmallDateTime

    DECLARE @CurrentDate SmallDateTime

    DECLARE @RunDateWoTime SmallDateTime

    SET @CurrentDate = GETDATE()

    SET @RunDate = DATEADD(day,-1,@CurrentDate)-- AS CurrentDate

    SELECT @RunDate AS RunDate

    -- Desired Result is the following:

    -- 2014-03-03 18:00

    Could do something like:

    DECLARE @RunDate SmallDateTime

    DECLARE @CurrentDate DATE

    DECLARE @RunDateWoTime SmallDateTime

    SET @CurrentDate = GETDATE()

    SET @RunDate = DATEADD(day,-1,@CurrentDate) + ' 18:00' -- AS CurrentDate

    SELECT @RunDate AS RunDate

  • ChrisM@Work (3/4/2014)


    SELECT DATEDIFF(dd,0,GETDATE())-1

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)

    SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))

    Use q3. q1 and q2 help explain the algorithm.

    Chris solution is obviously better than the below but if you do not want the seconds part then try below

    SELECT CONVERT(VARCHAR(10),GETDATE()-1,120) + ' 18:00' AS RunDate

  • Thank you very much everyone!

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • ChrisM@Work (3/4/2014)


    SELECT DATEDIFF(dd,0,GETDATE())-1

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)

    SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))

    Use q3. q1 and q2 help explain the algorithm.

    I can do it with just two date function calls:

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')

    I can also name that tune in 3 notes. πŸ˜›


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thank you very much for all of the help.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • dwain.c (3/4/2014)


    ChrisM@Work (3/4/2014)


    SELECT DATEDIFF(dd,0,GETDATE())-1

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)

    SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))

    Use q3. q1 and q2 help explain the algorithm.

    I can do it with just two date function calls:

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')

    I can also name that tune in 3 notes. πŸ˜›

    - and an implicit type conversion, which hides a note!

    DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (3/6/2014)


    dwain.c (3/4/2014)


    ChrisM@Work (3/4/2014)


    SELECT DATEDIFF(dd,0,GETDATE())-1

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)

    SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))

    Use q3. q1 and q2 help explain the algorithm.

    I can do it with just two date function calls:

    SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')

    I can also name that tune in 3 notes. πŸ˜›

    - and an implicit type conversion, which hides a note!

    DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))

    Where's the fun in that? πŸ˜›


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • awesome:cool:

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply