Formatting a datetime in UK format - Mmm dd YYYY

  • Hi

    I have a datetime field called Election date, defined as follows:

    DECLARE @ElectionDate datetime = '09/10/2018 11:00:01'

    How do i format this so its shown     as     'Oct 9 2018'     ?

  • Use the CONVERT function:

    This will do exactly what you asked for...

    SELECT CONVERT(CHAR(11),GETDATE(),100);

    This will add the typical comma before the year

    SELECT CONVERT(CHAR(12),GETDATE(),107);

    For more information on the CONVERT function, please see the following link...
    https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

    If someone suggests the use of the "new" FORMAT function, remind them that it takes 44 times longer to execute than CONVERT does and that code portability is a myth. 😉

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

Viewing 2 posts - 1 through 1 (of 1 total)

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