Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jjz
Starting Member
31 Posts |
Posted - 2012-02-22 : 02:07:08
|
| good Day everyoneI need your help.I am trying to calculate the trading days, excluding public holidays and weekends and I came through this function.however it returns a 0. May you kindly assist.ALTER FUNCTION [dbo].days_diff ( @STARTDATE datetime,@ENDDATE datetime ) RETURNS intASBEGINdeclare @i intDeclare @count intdeclare @diff intset @diff=datediff(d,@STARTDATE,@ENDDATE)set @i=0set @count=0While(@i<@diff)BeginSelect @count=@count+1 where datename(dw,dateadd(d,@i,@STARTDATE))not in('Saturday','Sunday')set @i=@i+1EndRETURN @countENDSelect dbo.days_diff(01/02/2012,22/02/2012) as diff |
|
|
anumodhc
Starting Member
13 Posts |
Posted - 2012-02-22 : 04:22:25
|
| DECLARE @StartDate datetime, @EndDate datetimeSELECT @StartDate = '01/01/2012',@EndDate = '01/31/2012' SELECT COUNT(dys) FROM (SELECT DATEADD(DAY, v.Number, @StartDate ) AS dys FROM master..spt_values AS v WHERE v.Type = 'P' ) result WHERE dys <= @EndDate AND DATEPART(DW,dys) NOT IN (1,7) This will exclude Saturday and Sundays.AnumodH |
 |
|
|
|
|
|
|
|