0
下午好第N个号码平日,在WHERE子句中使用函数返回月份
我打开了该功能,让我每月的第N号工作日,但挣扎在使用它我的WHERE子句来过滤掉在适用于此功能的日期上。功能代码是这样的:
CREATE FUNCTION [dbo].[fnGetNthWeekdayOfMonth]
(
@theDate DATETIME,
@theWeekday TINYINT,
@theNth SMALLINT
)
RETURNS DATETIME
BEGIN
RETURN (
SELECT theDate
FROM (
SELECT DATEADD(DAY, 7 * @theNth - 7 * SIGN(SIGN(@theNth) + 1) +(@theWeekday + 6 - DATEDIFF(DAY, '17530101', DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) % 7) % 7, DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) AS theDate
WHERE @theWeekday BETWEEN 1 AND 7
AND @theNth IN (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5)
) AS d
WHERE DATEDIFF(MONTH, theDate, @theDate) = 0
)
END
所以我想在我的WHERE子句中使用如上所述的函数。 我怎么做这是目前我得到的:
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
它工作正常,在日期字段格兰SELECT语句时。
在此先感谢。
你在哪里使用此功能 – Dbloch 2014-10-30 15:18:53
噢,我的,请建立一个日历表,请添加您的查询。 – 2014-10-30 15:36:52
日历桌将改变你的生活! – Zane 2014-10-30 15:55:04