我有一周的天数以及一年中的周数.如何在sql中计算当天的日期.例如,2014年2月22日的日数是第7天,也是一年的第8周.现在我如何从这些信息中计算出日期.紧急.请帮忙.我想要查询. 最佳答案 这是我的verison
create FUNCTION date_from_week_number_day
(
@year int = 2014
,@weeknumber int = 8
,@day int = 7
)
RETURNS date
AS
BEGIN
declare
@date date
,@first_date_of_year date
set @first_date_of_year = CONVERT(date,cast(@year as varchar(4))+'.1.1') --First calculate first day of year
set @date = dateadd(dd,@day -- we add days acoring of day of week
,dateadd(dd,-1*DATEPART(WEEKDAY,@first_date_of_year) --first day of year is not first day of week so we go back days how day has pass
, DATEADD(WEEK,@weeknumber-1,@first_date_of_year) --1. we go to week before last
)
)
return @date
END
你可以打电话给这个
SET DATEFIRST 7
select dbo.date_from_week_number_day(2014,8,7)