SOS 100分!
根据进厂日期,比如2006/06/13进厂,算出06/13到06/30之间,有几个星期六,
就是从进厂的日期开始到这月结束,有多少个星期6
在线等,很急!
------解决方案--------------------declare @dtBegin datetime
declare @dtEnd datetime
declare @t table(dt datetime,name varchar(20))
set datefirst 1
set @dtBegin= '2006/06/13 '
set @dtEnd=getdate()
while @dtBegin <@dtEnd
begin
if datepart(weekday,@dtBegin)=6
begin
insert @t select @dtBegin, '星期六 '
end
set @dtBegin=@dtBegin+1
end
select count(*) from @t
------解决方案--------------------declare @dtBegin datetime
declare @dtEnd datetime
declare @t table(dt datetime,name varchar(20))
set datefirst 1
set @dtBegin= '2006/06/13 '
set @dtEnd=getdate()
while @dtBegin <@dtEnd
begin
if datepart(weekday,@dtBegin)=6
begin
insert @t select @dtBegin, '星期六 '
end
set @dtBegin=@dtBegin+1
end
select count(*) from @t
------解决方案--------------------if object_id( 'tempdb..#tmp ') is not null
drop table #tmp
set datefirst 1
GO
select top 30 id = identity(int,1,1) into #tmp from syscolumns,sysobjects
declare @Begin datetime,@End datetime
set @Begin = '2006/06/13 '
set @End = '2006/06/30 '
select dateadd(day,id,@Begin) from #tmp where
dateadd(day,id,@Begin) < @End and datepart(weekday,dateadd(day,id,@Begin)) = 6
drop table #tmp
/*结果
2006-06-17 00:00:00.000
2006-06-24 00:00:00.000
*/
------解决方案--------------------declare @s smalldatetime
set @s= '2007-01-03 '
select top 31 id=identity(int,0,1) into # from syscolumns
select count(1) a from
(select id,@s a,dateadd(weekday,id,@s) b,datename(weekday,dateadd(weekday,id,@s)) c from # a where dateadd(day,id,@s) <=cast(convert(varchar(8),dateadd(mm,1,@s),120)+ '01 ' as smalldatetime)) xx
where c= '星期六 '
drop table #
------解决方案--------------------declare @a datetime
set @a= '2006/06/13 '
declare @b datetime
set @b= '2006/06/30 '
declare @i int
set @i=0
while @a <@b
begin
if datepart(dw,@a)=7
begin
set @i=@i+1
end
set @a=dateadd(d,1,@A)
end
select @i
------解决方案-------------------- -----------
2
(1 row(s) affected)
------解决方案--------------------如果你只要天数,我觉得以下的存储过程比较好:
create proc getSaturdayCount(
@dt datetime
)as
begin
declare @lastdateOfMonth datetime
--找出本月的最后一天
set @lastdateOfMonth=dateadd(day,-1,convert(char(8),dateadd(month,1,@dt),120)+ '1 ')
--本月的最后一天最后一天减去最近的一个星期六,得出最近的周六到月末的天数,再除以7,得有几个星期六
return (datediff(dd,@dt,@lastdateOfMonth)-(7-datepart(weekday,@dt)))/7
end
declare @r int
exec @r=getSaturdayCount '2006-6-13 '
print @r
------解决方案--------------------修改一下,忘了加上最近的一个星期六
------------------------------------------
create proc getSaturdayCount(
@dt datetime
)as
begin
declare @lastdateOfMonth datetime
--找出本月的最后一天
set @lastdateOfMonth=dateadd(day,-1,convert(char(8),dateadd(month,1,@dt),120)+ '1 ')