Home Forums SQL Server 2008 T-SQL (SS2K8) Need to find out whether date is of which data type RE: Need to find out whether date is of which data type

  • ISDATE

    Returns 1 if the expression is a valid date, time, or datetime value; otherwise, 0.

    ISDATE returns 0 if the expression is a datetime2 value.

    So as the first is a DateTimeOffset and the second is DateTime2, you would expect them to return 0.

    DECLARE @String VARCHAR(50) = '2013-01-08 15:44:12.20848489 -05:30'

    IF

    PATINDEX('%[A-Z]%',@String) = 0

    BEGIN

    SELECT

    CASE WHEN LEN(@String) = 10 THEN 'Date'

    WHEN LEN(@String) = 23 THEN 'DateTime'

    WHEN CHARINDEX('+',@String) > 0 THEN '+ Offset'

    WHEN CHARINDEX('-',REVERSE(@String)) <=6 THEN '- Offset'

    END AS WhatIsTheDataType

    END

    ELSE

    SELECT 'You have not supplied a valid date'