标签:
Function
从例子学习
drop function dbo.getWorkingDateCount; --getWorkingDateCount = functionName CREATE FUNCTION [dbo].[getWorkingDateCount] ( --这3个是parameter, 名字和类型 @fromDate date, @toDate date, @publicHolidayStrList nvarchar(500) ) RETURNS int --表明返回类型 AS BEGIN DECLARE @count INT = 0; --DECLARE 就是var WHILE @fromDate <= @toDate --while 就是for loop BEGIN DECLARE @ipos INT = 0; DECLARE @dateName nvarchar(50); --赋值一定要写set在前面, --CHARINDEX 是 indexOf --CONVERT 是 datetime to string , 126 是一个sql对日期格式的代号 refer : SET @ipos = CHARINDEX(CONVERT(VARCHAR(10),@fromDate,126), @publicHolidayStrList); SET @dateName = DATENAME(weekday, @fromDate); IF @ipos = 0 and @dateName != ‘Saturday‘ and @dateName !=‘Sunday‘ BEGIN SET @count = @count + 1; END SET @fromDate = DATEADD(day,1,@fromDate) END; RETURN @count; END select dbo.getWorkingDateCount(‘2015-11-01‘,‘2015-11-30‘,‘2015-11-07,2015-11-13‘) as ‘date‘;
上面是一个调用来获取工作天数的函数
标签:
原文地址:http://www.cnblogs.com/keatkeat/p/4963194.html