long date format

  • How do you format a field to return a long date format ie January 1, 2011?

  • Like this:

    DECLARE @Date DATETIME = GETDATE()

    SELECT CONVERT(VARCHAR(50),@Date,107) DayInWords

  • I tried it this way and got Jan 1, 2011.

    SELECT CONVERT(VARCHAR(50),mt.hiredate,107) FROM MyTable mt

  • isnt that what you wanted?

  • I need the month spelled out like January.

  • Try this then:

    DECLARE @Date DATETIME = GETDATE()

    SELECT CASE LEFT( CONVERT(VARCHAR(50),@Date,107),3)

    WHEN 'Jan' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Jan' ,'January')

    WHEN 'Feb' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Feb' ,'February')

    WHEN 'Mar' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Mar' ,'March')

    WHEN 'Apr' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Apr' ,'April')

    WHEN 'May' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'May' ,'May')

    WHEN 'Jun' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Jun' ,'June')

    WHEN 'Jul' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Jul' ,'July')

    WHEN 'Aug' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Aug' ,'August')

    WHEN 'Sep' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Sep' ,'Sepetember')

    WHEN 'Oct' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Oct' ,'October')

    WHEN 'Nov' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Nov' ,'November')

    WHEN 'Dec' THEN REPLACE( CONVERT(VARCHAR(50),@Date,107) ,'Dec' ,'December')

    END AS LongDate

  • Thank you 😀

  • Probably not important, but you can also make sure of the DATENAME function, that way sql will handle localizing the name of the month is that should ever be needed.

  • Lamprey13 (4/8/2011)


    Probably not important, but you can also make sure of the DATENAME function, that way sql will handle localizing the name of the month is that should ever be needed.

    I like it. Much simpler. Thanks for pointing it out.

    DECLARE @Date DATETIME = GETDATE() ;

    SELECT DATENAME(MONTH, @Date) + ' ' +

    DATENAME(DAY, @Date) + ', ' +

    DATENAME(YEAR, @Date) ;

    April 8, 2011

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • 😎

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

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