Convert DateTime to MMDDYY

  • I am new to SQL.

    Can you tell me how I would convert a DateTime field to MMDDYY without any / or .

    Example I would need the field to convert to 10409 for January 1, 2009.

    Any help would be greatly appreciated.

    Thanks

    Phillip Gatte

  • You could convert using style 10, then wrap that in a replace that gets rid of the punctuation.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY]]

    you would need to replace GETDATE() function with your date field. This will display as

    010710.

    Are the leading zeros a problem?

  • pgatte (1/7/2010)


    I am new to SQL.

    Can you tell me how I would convert a DateTime field to MMDDYY without any / or .

    Example I would need the field to convert to 10409 for January 1, 2009.

    Just checking: how do you get 10409 from Jan. 1 2009? (I'm assuming this is a typo.)

    Also, don't underestimate fixed characters for dates. 10409 can be interpreted as either Jan. 4, 2009 (1/04/09) or Oct. 4, 2009 (10/4/09). I'd suggest including a leading zero so it reads 010409 (and is consistent with the MMDDYY format). That way, there's no confusion.

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

  • Ray K (1/7/2010)


    pgatte (1/7/2010)


    I am new to SQL.

    Can you tell me how I would convert a DateTime field to MMDDYY without any / or .

    Example I would need the field to convert to 10409 for January 1, 2009.

    Just checking: how do you get 10409 from Jan. 1 2009? (I'm assuming this is a typo.)

    Also, don't underestimate fixed characters for dates. 10409 can be interpreted as either Jan. 4, 2009 (1/04/09) or Oct. 4, 2009 (10/4/09). I'd suggest including a leading zero so it reads 010409 (and is consistent with the MMDDYY format). That way, there's no confusion.

    Even then, if you don't know it is stored as MMDDYY, 010409 could still be confused as Jan 4, 2009 or Apr 1, 2009. Just something to be aware of depending on who will be seeing the data. You may want to be sure this is documented appropriately.

  • To even confuse a little more, 010409 might be as well interpreted as April 9th 2001...

    I probably would even include the format in the string and truncate it where needed (e.g. 010409MDY). I don't expect it to be used in any calculation...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thank you! That worked!

    Phillip

Viewing 7 posts - 1 through 6 (of 6 total)

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