UFN_VALIDSQLFUZZYDATEWITHMONTHDAY

Validates fuzzy dates with allow month day are valid.

Return

Return Type
bit

Parameters

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

Definition

Copy


create function dbo.UFN_VALIDSQLFUZZYDATEWITHMONTHDAY(@SQLFUZZYDATE 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(@SQLFUZZYDATE) <> 8    
        return 0;

    if isnumeric(@SQLFUZZYDATE) = 0
        return 0;

    set @YEAR = cast(left(@SQLFUZZYDATE, 4) as smallint);
    set @MONTH = cast(substring(@SQLFUZZYDATE, 5, 2) as tinyint);
    set @DAY = cast(right(@SQLFUZZYDATE, 2) as tinyint);

     --SQLServer allows years between 1753 and 9999     

     if @YEAR > 9999
        return 0;

    --we only require a month for fuzzy dates if the day is specified

     if @YEAR < 1753 and @YEAR <> 0
        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 @YEAR > 0 and @DAY > dbo.UFN_DAYSINMONTH(@MONTH, @YEAR)
        return 0;

    return 1;

end