Date Only

  • Is there away to only return the date portion of the a datetime?

  • SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))

     

    IIRC, there's a slightly faster version to make this conversion (like 5% faster), but I can't recall what it is.  Anyhow if you are not running this of more than a few million rows, you probably won't see any difference.

  • Not sure if this is it but as I recall I think this is the fastest:

    SELECT DATEADD(dd, CONVERT(FLOAT, GETDATE()), 0)

  • I can foresee hundreds of replies to this thread...so I'll give you the best answer now before anyone beats me to it. 😛

    select convert(varchar(10), getdate(), 101)

    Cheers!

    -Ben


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Ben, run all 3 versions of your code and see for yourself.  Even at 10 000 rows, the convert will most likely be constantly slower.  At millions it's not even worth testing.

  • A couple of things I forgot to mention.

    It can't return the time in the format 00:00:00.000 and the date must be in format YYYY/MM/DD.

  • A date is a place in time, hence you can't have a date without time.  If you don't want to display the time you can either have the application hide that part, or use the convert solution posted.

  • select convert(varchar(10),getdate(),111) will give you the correct result. As posted by Ninja's RGR'us this may slow things down a bit when being performed on millions of rows.

    cheers,

    Ben


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Thanks everyone!

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

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