SQL Server - where + TVF/SVF, cross-apply, T-SQL -
i trying create sql pull out data based on date, having troble use clause need have following table , tvf:
/* dbo.weeks -> column(weeknum), column(date) **date fridays** */ weeknum date 1 2001-09-21 2 2001-09-28 3 2001-10-05 ... .... /* fnbenchmark(@weeknum) -> display benchmark data based on weeknum */
here code:
select p.weeknum, p.date, q.companyid, q.index dbo.weeks p cross apply dbo.fnbenchmark(p.weeknum) q date = '2001-09-21' /* here wish add above code don't know how */ if date in dbo.weeks column (date) --if date on clause friday , on dbo.weeks table run above code else change date lastfridaydate --if date on clause sat~thur, use previous friday's date run above
for example, let's 2001-09-21 friday, if enter 2001-09-24 on clause, want sql run "where date = "2001-09-21" since 2001-09-24 entered not friday"
maybe should create function (svf?) return previous friday's date based on parameter @date entered? , use:
where date = fnfridaycheck(@date entered)
mayby use
datepart(dw,getdate())
and storedprocedure or query example
if datepart(dw,getdate()) = 6 begin if it's friday (because 6 in dateprart points firday) end else begin else end
Comments
Post a Comment