当前位置: 代码迷 >> Sql Server >> 关于SQL求指定月度天数及工作日天数
  详细解决方案

关于SQL求指定月度天数及工作日天数

热度:21   发布时间:2016-04-27 11:04:06.0
关于SQL求指定月份天数及工作日天数
sql 求指定月份天数?
SQL 求指定月份工作日天数?

------解决方案--------------------
SQL code
--指定月份天数declare @m varchar(7)set @m='2012-08'select datediff(day,@m+'-01',dateadd(month,1,@m+'-01'))--指定月份工作天数declare @dt datetime,@dt2 datetime,@i intset @dt=convert(datetime,@m+'-01')set @dt2=dateadd(month,1,@dt)while @dt<@dt2begin    if datepart(weekday,@dt)<>1 and datepart(weekday,@dt)<>6        set @i=isnull(@i,0)+1    set @dt=dateadd(day,1,@dt)    endselect @i
------解决方案--------------------
SQL code
create  function getWorkDays( @year int , @month int )returns int as begin     declare @CountDay int      declare @temptime datetime    set @CountDay = 0     set @temptime = convert(varchar,@year) + '-'+ convert(varchar,@month) + '-01'      while(MONTH(@temptime) = @month)     begin         if DATEPART(weekday,@temptime) <>1 and DATEPART(weekday,@temptime)<>7          begin             set @CountDay = @CountDay + 1            End        set @temptime = DATEADD(day,1,@temptime)     end    return @CountDayend --调用select dbo.getWorkDays(2009,7)
  相关解决方案