Date Format (mm/dd/yyyy hh:mm), no seconds or milliseconds

  • Rohit Machhi

    Newbie

    Points: 1

    select FORMAT(Convert(datetime,GETDATE (),103),'MM/dd/yyyy HH:mm')

  • Jeff Moden

    SSC Guru

    Points: 993661

    Rohit Machhi wrote:

    select FORMAT(Convert(datetime,GETDATE (),103),'MM/dd/yyyy HH:mm')

     

    While that's incredibly simple, as it was designed to be, FORMAT has been tested and documented as being 44 times slower than even poor renditions.  Until MS fixes the performance issues with FORMAT (and they won't), don't use FORMAT.

    --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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • Jason A. Long

    SSC-Insane

    Points: 23403

    Because BOL doesn't list/describe all of the available CONVERT styles...

    DECLARE @n INT = 0, @v nvarchar(100);
    WHILE @n < 10000
    BEGIN
    BEGIN TRY
    SELECT @v = CONCAT(@n, N' ', CONVERT(NVARCHAR(50), GETDATE(), @n));
    RAISERROR(@v, 0, 1);
    SET @n += 1;
    END TRY
    BEGIN CATCH
    SET @n += 1;
    END CATCH;
    END;

     

Viewing 3 posts - 16 through 18 (of 18 total)

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