我试图创建一个SQL来根据日期提取数据,现在有权使用where子句来获取我真正需要的东西 我有下表和TVF:
/* dbo.weeks -> column(weeknum), column(date) **date is all Fridays** */
weeknum date
1 2001-09-21
2 2001-09-28
3 2001-10-05
... ....
/* fnBenchMark(@weeknum) -> display benchmark data based on weeknum */
这是我的代码:
SELECT p.weeknum, p.date, q.companyid, q.index
FROM dbo.Weeks AS p
cross apply
dbo.fnBenchmark(p.weeknum) as q
where date = '2001-09-21'
/* Here is what I wish to add to above code but don't know how to do it */
if
date in dbo.Weeks column (date) --if date on where clause is a Friday and it is on dbo.weeks table
then run above code
else
change date to LastFridayDate --if date on where clause is Sat~Thur, then use previous Friday's date
then run above
例如,假设2001-09-21是星期五,如果我在where子句中输入 2001-09-24 ,我希望SQL运行"其中date =" 2001-09-21 "自2001-09-24以来,我进入的不是星期五"
也许我应该创建另一个函数(SVF?),根据我输入的参数@date返回上周五的日期?然后使用:
where date = fnFridayCheck(@date I entered)
答案 0 :(得分:0)
datepart(dw,getdate())
而不仅仅是storedprocedure或仅仅是一个查询
IF datepart(dw,getdate()) = 6
BEGIN
something if it's a friday (because 6 in dateprart points to firday)
END
ELSE
BEGIN
something else to do
END