convert datetime

  • Hi guys,

    What is the best way to convert 7/29/2003 8:02:22 AM into this format : 20030729080221 ?

    Corrently i am using :

    SELECT SUBSTRING(replace(replace(replace(convert(varchar,completeDT,120),'-',''),' ',''),':',''),1,14)

    But when it converts i got 1 sec less as in above example

    Thank you

  • Fun one.

    
    
    DECLARE @date DATETIME
    SET @date = '7/29/2003 8:02:22 AM'
    --
    SELECT CAST(CONVERT(CHAR(4), YEAR(@date)) +
    RIGHT('0' + CONVERT(VARCHAR, MONTH(@date)), 2) +
    RIGHT('0' + CONVERT(VARCHAR, DAY(@date)), 2) +
    RIGHT('0' + CONVERT(VARCHAR, DATEPART(hour, @date)), 2) +
    RIGHT('0' + CONVERT(VARCHAR, DATEPART(minute,@date)), 2) +
    RIGHT('0' + CONVERT(VARCHAR, DATEPART(second,@date)), 2) AS VARCHAR(14))

    Edited by - jpipes on 07/29/2003 2:29:41 PM

  • Declare @d Datetime

    Set dateformat mdy

    Select @d='7/29/2003 8:02:22 AM '

    Select @d

    SELECT SUBSTRING(replace(replace(replace(convert(varchar,@d,120),'-',''),' ',''),':',''),1,14)

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

    2003-07-29 08:02:22.000

    (1 row(s) affected)

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

    20030729080222

    (1 row(s) affected)

    SQL 2000 Service Pack 3

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

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