SQL日期计算问题

M1234 2007-03-14 09:28:02
如何取得某一年,第N个月份,第M个星期日的日期,例如 2000年5月的第3个星期日

CREATE FUNCTION fn_getDate
(
@p_Year SMALLINT,
@p_Month TINYINT,
@p_N TINYINT
)
RETURNS SMALLDATETIME
AS
BEGIN
...
END
...全文
318 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
M1234 2007-03-14
  • 打赏
  • 举报
回复
多谢,我试试看
playwarcraft 2007-03-14
  • 打赏
  • 举报
回复
忘了,最好在之前對輸入的參數加以判斷
筆如@p_N between 1 and 5
@p_Month between 1 and 12 ...
playwarcraft 2007-03-14
  • 打赏
  • 举报
回复
--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
*/

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧