Home Forums Data Warehousing Integration Services how to check whether in the given column we have values in date format RE: how to check whether in the given column we have values in date format

  • Lowell (4/17/2013)


    there is an ISDATE function in SQL as well;

    here's an example base don your post:

    /*

    /*

    Eid Variable Date? Converted?

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

    1 na 0 NULL

    2 none 0 NULL

    3 4/18/2013 1 2013-04-18 00:00:00.000

    4 18/2013 0 NULL

    5 2013 1 2013-01-01 00:00:00.000

    6 9999999 0 NULL

    */

    WITH MySampleData (Eid,Variable)

    AS

    (

    SELECT '1','na' UNION ALL

    SELECT '2','none' UNION ALL

    SELECT '3','4/18/2013' UNION ALL

    SELECT '4','18/2013' UNION ALL

    SELECT '5','2013' UNION ALL

    SELECT '6','9999999'

    )

    SELECT

    MySampleData.*,

    ISDATE(Variable) AS [Date?],

    CASE

    WHEN ISDATE(Variable) = 1

    THEN CONVERT(datetime,Variable)

    ELSE NULL

    END As [Converted?]

    FROM MySampleData

    Although it's not a flaw, you've just demonstrated a "problem" with IsDate that's similar to people thinking that IsNumeric means "IsAllDigits". It'll take a whole lot more than just mm/dd/yyyy as a date. The OP's requirement is to capture only those things that look like mm/dd/yyyy as well as being a valid data.

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