--try:GO
CREATE FUNCTION fn_getDate
(
@p_Year SMALLINT,
@p_Month TINYINT,
@p_N TINYINT
)
RETURNS SMALLDATETIME
AS
BEGIN
declare @i int,@date SMALLDATETIME
select @date=convert(smalldatetime,rtrim(@p_Year)+'-'+rtrim(@p_Month)+'-1')
set @i=(@p_N-1)*7
while @i<@p_N*7
begin
if datepart(weekday,dateadd(day,@i,@date))=1
begin
break
end
else
set @i=@i+1
end
return dateadd(day,@i,@date)
END
GO/*
select dbo.fn_getDate(2007,3,2)
---------------------------------
2007-03-11 00:00:00
*/
CREATE FUNCTION fn_getDate
(
@p_Year SMALLINT,
@p_Month TINYINT,
@p_N TINYINT
)
RETURNS SMALLDATETIME
AS
BEGIN
declare @i int,@date SMALLDATETIME
select @date=convert(smalldatetime,rtrim(@p_Year)+'-'+rtrim(@p_Month)+'-1')
set @i=(@p_N-1)*7
while @i<@p_N*7
begin
if datepart(weekday,dateadd(day,@i,@date))=1
begin
break
end
else
set @i=@i+1
end
return dateadd(day,@i,@date)
END
GO/*
select dbo.fn_getDate(2007,3,2)
---------------------------------
2007-03-11 00:00:00
*/
筆如@p_N between 1 and 5
@p_Month between 1 and 12 ...