差不多,我试图在假期之间的两天之间得到差异。我已经对假期日进行了硬编码,这对我来说现在很好,但似乎可以让我的代码过滤掉那些日子。任何提示或建议将不胜感激。
ALTER FUNCTION [dbo].[ufn_number_of_business_days]
(
@vstartdate as datetime,
@venddate as datetime
)
RETURNS int
AS
BEGIN
declare @vnumberofdays as INT
if @vstartdate >= @venddate
set @vnumberofdays = 0
ELSE
IF
REPLACE(CONVERT(VARCHAR(8),CAST (@vstartdate AS DATE),110), '-', '') IN
('01012007', '05282007', '07042007', '09032007', '11222007',
'11232007', '12242007', '12252007', '12312007', '05262008',
'07032008', '07042008', '09012008', '11272008', '11282008',
'12252008', '12262008', '01012009', '01022009', '05252009',
'07032009', '09072009', '11262009', '11272009', '12252009',
'01012010', '05312010', '07022010', '07052010', '09062010',
'11252010', '11262010', '12242010', '12272010', '12282010',
'12292010', '12302010', '12312010', '05302011', '07012011',
'07042011', '09052011', '11242011', '11252011', '12262011',
'12272011', '12282011', '12292011', '12302011', '01022012',
'05282012', '07042012', '07052012', '07062012', '09032012',
'11222012', '11232012', '12242012', '12252012', '12262012',
'12272012', '12282012', '12312012', '01012013', '05272013',
'07042013', '07052013', '09022013', '11282013', '11292013',
'12242013', '12252013', '12262013', '12272013', '12302013',
'12312013', '01012014', '05262014', '07032014', '07042014',
'09012014', '11272014', '11282014', '12242014', '12252014',
'12262014', '12292014', '01012015', '05252015')
BEGIN
set @vstartdate = 0
END
ELSE
IF
REPLACE(CONVERT(VARCHAR(8),CAST (@venddate AS DATE),110), '-', '') IN
('01012007', '05282007', '07042007', '09032007', '11222007',
'11232007', '12242007', '12252007', '12312007', '05262008',
'07032008', '07042008', '09012008', '11272008', '11282008',
'12252008', '12262008', '01012009', '01022009', '05252009',
'07032009', '09072009', '11262009', '11272009', '12252009',
'01012010', '05312010', '07022010', '07052010', '09062010',
'11252010', '11262010', '12242010', '12272010', '12282010',
'12292010', '12302010', '12312010', '05302011', '07012011',
'07042011', '09052011', '11242011', '11252011', '12262011',
'12272011', '12282011', '12292011', '12302011', '01022012',
'05282012', '07042012', '07052012', '07062012', '09032012',
'11222012', '11232012', '12242012', '12252012', '12262012',
'12272012', '12282012', '12312012', '01012013', '05272013',
'07042013', '07052013', '09022013', '11282013', '11292013',
'12242013', '12252013', '12262013', '12272013', '12302013',
'12312013', '01012014', '05262014', '07032014', '07042014',
'09012014', '11272014', '11282014', '12242014', '12252014',
'12262014', '12292014', '01012015', '05252015')
set @venddate = 0
ELSE
BEGIN
SET @vnumberofdays = replace(convert(int, @vstartdate) - convert(int, @venddate), '-', '')
END
RETURN @vnumberofdays
END
答案 0 :(得分:0)
我只想为Holidays
创建另一个表,然后你可以做一个简单的DATEDIFF
并使用它减去:
DECLARE @start DATETIME;
DECLARE @end DATETIME;
SELECT DATEDIFF(DAY, @start, @end)
- (SELECT COUNT(DISTINCT HolidayDate)
FROM Holidays
WHERE HolidayDate BETWEEN @start AND @end)
我想你也可以使用一个表值变量,但我更喜欢将它作为一个单独的表。这种方式更干净。表值函数也可以工作。