UFN_YMD

Return

Return Type
datetime

Parameters

Parameter Parameter Type Mode Description
@year int IN
@month int IN
@day int IN

Definition

Copy


CREATE function dbo.UFN_YMD(@year int, @month int, @day int)
returns datetime

-- Returns a date value corresponding to the given year, month, and day of the month. 

-- if the month is outside the range 1-12, the year is adjusted accordingly.

-- if the day is outside the range valid for the specified month, the month is adjusted


begin
    declare @sDate varchar(20)
    declare @dtDate datetime

    if @year < 1753  set @Year = 1753
    Set @sDate = '1-1-' + cast(@year as varchar(4))
    Set @dtDate = cast(@sDate as datetime)

    if @month > 0 begin
        Set @dtDate = dateadd(m,@month-1,@dtDate)
    end

    if @day > 0 begin
        Set @dtDate = dateadd(d,@day-1,@dtDate)
    end

    return @dtDate
end