AmitayD wrote:
Hmmm, it was trickier than i thought, but eventually i came up with this one too:
SELECT c1.DT,
c1.[M],
c1.[D],
CAST(FLOOR(CAST(@Date AS FLOAT))AS DATETIME) Today
FROM Calendar c1
WHERE
c1.isWeekDay = 1
AND
(
SELECT count(*)
FROM Calendar c2
WHERE c2.dt <= c1.dt
AND c2.dt >= @Date
AND c2.isWeekDay = 1) = @Days
Which is about 100 times slower (still in the few ms), but it doesn't use an mssql function
Does anyone have an idea how to do this efficiently without enumerating the rows the way ROW_NUMBER() does? (not that i think there's any major reason not too)
Amitay
Thanks for the reply and the query. I did finally get this figured out using the following queries.
CREATE FUNCTION dbo.GetCalendarDayXDaysFromNow(@CurrentDate datetime, @Days smallint)
RETURNS datetime
BEGIN
-- VERSION: 200806121437
DECLARE
@VisitDate datetime,
@JulianDayToday smallint
-- SET @CurrentDate = GETDATE()
-- SET @Days = 7
--// Get Julian Day Today
SET @JulianDayToday = (SELECT JulianBusinessDay FROM SharedInfo.dbo.Calendar WHERE [DT] = CAST(FLOOR(CAST(@CurrentDate AS FLOAT)) AS DATETIME))
SELECT
@VisitDate = [DT]
FROM
SharedInfo.dbo.Calendar
WHERE
JulianBusinessDay = @JulianDayToday + @Days
AND
IsWeekDay = 1
RETURN @VisitDate
END
DECLARE
@Date datetime
SET @Date = GETDATE()
SELECT
vr.RequestorFirstName,
vr.RequestorLastName,
vr.FacilityName,
sv.EventName,
vr.StartTime,
VisitDate = CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, vr.StartTime)))
FROM
dbo.VisitRequest vr
LEFT JOIN
dbo.ScheduledVisit sv ON vr.ScheduledVisitID = sv.ScheduledVisitID
WHERE
CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, vr.StartTime))) = CDGVisitProgram.dbo.GetCalendarDayXDaysFromNow(@Date, 5)
AND
(sv.Canceled IS NULL OR sv.Canceled = 0)
ORDER BY
vr.StartTime
Now I just have to figure out how to call CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, vr.StartTime))) as a LLBLGen DbFunctionCall. I was using DAY(vr.StartTime) as a DbFunctionCall but I am not sure that calling multiple nested functions will work.