RE:

  • Hi,

    Is there a way to convert VARCHAR to DataType to DateTime

    Eg: Select CAST('22/10/2007' AS DateTime)

    Thanks

    Jack

     

     

  • CAST OR CONVERT Can be used

    DECLARE @AA VARCHAR(25)

    SET @AA = '01/01/2006'

    IF ISDATE(@AA) = 1

    SELECT CONVERT(DATETIME, @AA) AADate

    ELSE

    PRINT 'Not Valid Date'

    SET @AA = '01/01/20062'

    IF ISDATE(@AA) = 1

    SELECT CONVERT(DATETIME, @AA) AADate

    ELSE

    PRINT 'Not Valid Date'

    Regards,
    gova

  • Thanks Buddy that helps.. Appreciate your quick response too.

  • Jack,

    It appears that you want to convert the dd/mm/yyyy format in which case, the examples posted, so far, will reject a large portion of your dates.  If that's true, then try this, instead...

     SELECT CONVERT(DATETIME,somedatecol,103)

        FROM sometable

    "103" is the dd/mm/yyyy format for CONVERT...

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

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

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