locked
trying to get today's date RRS feed

  • Question

  • I need a way to get todays date, but in the format '2010-04-27 00:00:00'

    I can use getdate() to get the full date and time, but I can't get JUST the date part of it. I thought maybe I could break it up with

    select (year(getdate())+'-'+month(getdate())+'-'+day(getdate()))+' 00:00:00'

    But this just gives an error about not being able to convert 00:00:00 to an integer. So I tried

    CONVERT(SMALLDATETIME,(select (year(getdate())+'-'+month(getdate())+'-'+day(getdate()))+' 00:00:00')

    But this failed as well.

    I tried

    select (year(getdate())+'-'+month(getdate())+'-'+day(getdate()))

    but this gave the result of '2041'

     

    I'm sure there must be a way to extract the date, but I just can't find it.

    Tuesday, April 27, 2010 3:22 PM

Answers

  • SELECT CONVERT(VARCHAR(20), GETDATE(), 20)
    • Proposed as answer by Vatsal Mayer Tuesday, April 27, 2010 3:27 PM
    • Marked as answer by iGGt Tuesday, April 27, 2010 3:40 PM
    Tuesday, April 27, 2010 3:27 PM
  • Try:

    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)

     


    Plamen Ratchev
    • Proposed as answer by Naomi N Tuesday, April 27, 2010 3:42 PM
    • Marked as answer by iGGt Wednesday, April 28, 2010 7:51 AM
    Tuesday, April 27, 2010 3:40 PM

All replies

  • SELECT CONVERT(VARCHAR(20), GETDATE(), 20)
    • Proposed as answer by Vatsal Mayer Tuesday, April 27, 2010 3:27 PM
    • Marked as answer by iGGt Tuesday, April 27, 2010 3:40 PM
    Tuesday, April 27, 2010 3:27 PM
  • cheers,

    I knew there had to be an easy way to do it. I have used

     

    SELECT CONVERT(VARCHAR(20), GETDATE(), 23)

    as this is the closest to what I needed

     

    '2010-04-27'

    Tuesday, April 27, 2010 3:40 PM
  • Try:

    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)

     


    Plamen Ratchev
    • Proposed as answer by Naomi N Tuesday, April 27, 2010 3:42 PM
    • Marked as answer by iGGt Wednesday, April 28, 2010 7:51 AM
    Tuesday, April 27, 2010 3:40 PM
  • cheers,

     

    another good solution.

    Wednesday, April 28, 2010 7:51 AM