help with date formatting

  • How do I format the result returned by getdate() into mm/dd/yy format?

    I've gotten as far as:

    PRINT CONVERT(DATETIME,GETDATE(),101)

    but that gives me the time as well.

    thanks in advance

    Billy

  • nevermind...

    PRINT CONVERT(varchar(8),GETDATE(),101)

  • quote:


    How do I format the result returned by getdate() into mm/dd/yy format?

    I've gotten as far as:

    PRINT CONVERT(DATETIME,GETDATE(),101)

    but that gives me the time as well.

    thanks in advance

    Billy


    -------------------------

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Try this out......

    -- mm/dd/yy format

    select convert(char(8),getdate(),1)

    -- mm/dd/yyyy format

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

    -------------------------

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • thanks...

    it shows that my previous reply needs to be correct...

    PRINT CONVERT(varchar(8),GETDATE(),101)

    ...does not give you the correct date because the result requires 10 chars, not just 8.

    Billy

  • Actually you do not have to specify a size on the datatype char or varchar

    CONVERT(CHAR,GETDATE(),101) will work just fine.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • Antares686 is correct but be aware that if you do not specify a length for varchar or char data types the length will default to 30 bytes, usually not a problem with varchar but may be a problem with char.

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

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