How to Convert Varchar to datetime datatype

  • I have carchar datatype in the following format

    '09-06-2005 00:00:00'

    is there a way to convert this value to datetime type as I need to calculate the datediff between the current date and the above date.

    Thanks for you help.

    "More Green More Oxygen !! Plant a tree today"

  • declare @test-2 varchar(20)

    set @test-2 = '09-06-2005 00:00:00'

    select datediff(month, cast(@test as datetime),getdate())

    select cast(@test as datetime)

  • Minaz Amin (10/23/2009)


    I have carchar datatype in the following format

    '09-06-2005 00:00:00'

    is there a way to convert this value to datetime type as I need to calculate the datediff between the current date and the above date.

    Thanks for you help.

    No conversion necessary... an implit conversion will take place in both DATEDIFF and DATEADD.

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

  • Thank you all for helping me out. code is absolutly running fine.:-D

    "More Green More Oxygen !! Plant a tree today"

  • what about this?

    select datediff(d,getdate(),convert (datetime,'09-06-2005 00:00:00',101))

    Kindest Regards,
    Paarthasarathy
    Microsoft Certified Technology Specialist
    http://paarthasarathyk.blogspot.com

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

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