UFN_VALIDSQLFUZZYDATE

Return

Return Type
bit

Parameters

Parameter Parameter Type Mode Description
@sSQLFuzzyDate char(8) IN

Definition

Copy



create function dbo.UFN_VALIDSQLFUZZYDATE(@sSQLFuzzyDate char(8))
returns bit with execute as caller
as
begin

    declare @Year smallint
    declare @Month tinyint
    declare @Day tinyint

    --8 chars are required
    if len(@sSQLFuzzyDate) <> 8 
        return 0

    if isnumeric(@sSQLFuzzyDate) = 0 
        return 0

    set @Year = cast(left(@sSQLFuzzyDate, 4) as smallint)
    set @Month = cast(substring(@sSQLFuzzyDate, 5, 2) as tinyint)
    set @Day = cast(right(@sSQLFuzzyDate, 2) as tinyint)

    --SQLServer allows years between 1753 and 9999
    if @Year > 9999
        return 0
    if @Year < 1753
        return 0

    --we only require a month for fuzzy dates if the day is specified
    if @Month > 12
        return 0
    if (@Month < 1) and (@Day > 0)
        return 0

    --day is optional
    if @Day < 0 
        return 0
    if @Day > dbo.UFN_DAYSINMONTH(@Month, @Year)
        return 0

    return 1

end