请问下。。sql怎么判断一个个月的第一周,第二周,第三周
请问下。。sql怎么判断一个个月的第一周,第二周,第三周???
[最优解释]
create function fn_getwk(
@year int,--输入显示的年份
@month int, --输入显示的月份
@type bit=0 --指定每周的第一天,默认为星期日(中国习惯),如果参数为1就是星期一为每周的第一天
) returns @t table(
周数 nvarchar(10),
开始日期 varchar(10),
结束日期 varchar(10)
) as
begin
declare @d datetime
set @d=dateadd(wk,datediff(wk,'1900',cast(ltrim(@year*10000+@month*100+1) as datetime)),'1900')+@type
;with t as
(
select top (datediff(dd,ltrim(@year*10000+@month*100+1),ltrim(@year*10000+(@month+1)*100+1)))
[date]=cast(ltrim(@year*10000+@month*100+1) as datetime)-1
+row_number()over(order by getdate())
from sysobjects
)
insert @t
select
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end AS 周数,
convert(varchar,min([date]),23) 开始日期,
convert(varchar,max([date]),23) 结束日期
from t
group by
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end
order by 2
return
end
GO
select * from fn_getwk(2010,5,0)
/*
周数 开始日期 结束日期
---------- ---------- ----------
第一周 2010-05-01 2010-05-01
第二周 2010-05-02 2010-05-08
第三周 2010-05-09 2010-05-15
第四周 2010-05-16 2010-05-22
第五周 2010-05-23 2010-05-29
第六周 2010-05-30 2010-05-31
(6 行受影响)
*/
select * from fn_getwk(2010,5,1)
/*
周数 开始日期 结束日期
---------- ---------- ----------
第一周 2010-05-01 2010-05-02
第二周 2010-05-03 2010-05-09
第三周 2010-05-10 2010-05-16
第四周 2010-05-17 2010-05-23
第五周 2010-05-24 2010-05-30
第六周 2010-05-31 2010-05-31
(6 行受影响)
*/
You may also need to use
Set datefirst {number} to specify the starting day of a week (i.e., whether a week starts on a sunday or any other days. To be sure, I normally run Set Datefirst 1 to ensure that SQL Server counts Monday as the first day of the week).
[其他解释]
/*
功能:2000当中绘画日历
*/
DECLARE @Year nvarchar(4)
DECLARE @YearMonth nvarchar(7) --月份
DECLARE @strTop nvarchar(200)
DECLARE @ForI INT,@ForYear INT ,@MaxDay INT
DECLARE @RowX INT --行位置
DECLARE @strWeekDayList nvarchar(20)
DECLARE @strPrint nvarchar(300)
-- ======================================
SET @Year='2008' --请在这里输入年份
-- ======================================
SET @strTop= '日'+char(9)+'一'+char(9)+'二' +char(9)+'三'++char(9)+'四'++char(9)+'五'++char(9)+'六' +char(13)+
'───────────────────────────'
SET @strWeekDayList='日一二三四五六'
SET @ForYear=1
WHILE @ForYear<=12 --1月份至12月份
BEGIN
SET @YearMonth=@Year + '-' +CAST( @ForYear AS nvarchar(2))
SET @MaxDay=DAY(DATEADD(Day,-1,DATEADD(Month,1,@YearMonth+'-01')))
SET @RowX=CHARINDEX(RIGHT(DATENAME(WeekDay,@YearMonth+'-01'),1),@strWeekDayList)-1
SET @strPrint=''
SET @ForI=1
WHILE @ForI<=@RowX --构造1号的位置
BEGIN
SET @strPrint=@strPrint+CHAR(9)
SET @ForI=@ForI+1
END
SET @ForI=1
WHILE @ForI<=@MaxDay --构造2号到月底的位置
BEGIN
SET @strPrint=@strPrint+CAST(@ForI AS nvarchar(2)) +Char(9)
SET @RowX=@RowX+1
SET @ForI=@ForI+1
IF (@RowX%7=0)
BEGIN
SET @RowX=0
SET @strPrint=@strPrint+CHAR(13)
END
END
SET @ForYear=@ForYear+1
-- 打印输出一个月的结果
PRINT '━━━━━━━━━━━━━━━━━━━━━━━━━━━'
PRINT +Char(9)++Char(9)+' '+@YearMonth+CHAR(10)
PRINT @strTop
PRINT @strPrint+CHAR(10)
END
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-1
日一二三四五六
───────────────────────────
12345
6789101112
13141516171819
20212223242526
2728293031
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-2
日一二三四五六
───────────────────────────
12
3456789
10111213141516
17181920212223
242526272829
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-3
日一二三四五六
───────────────────────────
1
2345678
9101112131415
16171819202122
23242526272829
3031
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-4
日一二三四五六
───────────────────────────
12345
6789101112
13141516171819
20212223242526
27282930
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-5
日一二三四五六
───────────────────────────
123
45678910
11121314151617
18192021222324
25262728293031
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-6
日一二三四五六
───────────────────────────
1234567
891011121314
15161718192021
22232425262728
2930
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-7
日一二三四五六
───────────────────────────
12345
6789101112
13141516171819
20212223242526
2728293031
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-8
日一二三四五六
───────────────────────────
12
3456789
10111213141516
17181920212223
24252627282930
31
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-9
日一二三四五六
───────────────────────────
123456
78910111213
14151617181920
21222324252627
282930
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-10
日一二三四五六
───────────────────────────
1234
567891011
12131415161718
19202122232425
262728293031
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-11
日一二三四五六
───────────────────────────
1
2345678
9101112131415
16171819202122
23242526272829
30
━━━━━━━━━━━━━━━━━━━━━━━━━━━
2008-12
日一二三四五六
───────────────────────────
123456
78910111213
14151617181920
21222324252627
28293031
*/
select datename(weekday,getdate())
--星期日
2010-5
日一二三四五六
───────────────────────────
1
2345678
9101112131415
16171819202122
23242526272829
3031
declare @date datetime ,@date2 datetime
select @date='2010-05-01' ,@date='2010-05-10'
select case datediff(dd,@date,getdate())/7
when 0 then '第一周'
when 1 then '第一周'
when 2 then '第二周'
when 3 then '第三周'
when 4 then '第四周'
else '第五周'
end
------
第一周
(1 行受影响)
/*
功能:2000当中绘画日历
*/
DECLARE @Year nvarchar(4)
DECLARE @YearMonth nvarchar(7) --月份
DECLARE @strTop nvarchar(200)
DECLARE @ForI INT,@ForYear INT ,@MaxDay INT
DECLARE @RowX INT --行位置
DECLARE @strWeekDayList nvarchar(20)
DECLARE @strPrint nvarchar(300)
-- ======================================
SET @Year='2008' --请在这里输入年份
-- ======================================
SET @strTop= '日'+char(9)+'一'+char(9)+'二' +char(9)+'三'++char(9)+'四'++char(9)+'五'++char(9)+'六' +char(13)+
'───────────────────────────'
SET @strWeekDayList='日一二三四五六'
SET @ForYear=1
WHILE @ForYear<=12 --1月份至12月份
BEGIN
SET @YearMonth=@Year + '-' +CAST( @ForYear AS nvarchar(2))
SET @MaxDay=DAY(DATEADD(Day,-1,DATEADD(Month,1,@YearMonth+'-01')))
SET @RowX=CHARINDEX(RIGHT(DATENAME(WeekDay,@YearMonth+'-01'),1),@strWeekDayList)-1
SET @strPrint=''
SET @ForI=1
WHILE @ForI<=@RowX --构造1号的位置
BEGIN
SET @strPrint=@strPrint+CHAR(9)
SET @ForI=@ForI+1
END
SET @ForI=1
WHILE @ForI<=@MaxDay --构造2号到月底的位置
BEGIN
SET @strPrint=@strPrint+CAST(@ForI AS nvarchar(2)) +Char(9)
SET @RowX=@RowX+1
SET @ForI=@ForI+1
IF (@RowX%7=0)
BEGIN
SET @RowX=0
SET @strPrint=@strPrint+CHAR(13)
END
END
SET @ForYear=@ForYear+1
-- 打印输出一个月的结果
PRINT '━━━━━━━━━━━━━━━━━━━━━━━━━━━'
PRINT +Char(9)++Char(9)+' '+@YearMonth+CHAR(10)
PRINT @strTop
PRINT @strPrint+CHAR(10)
END
create function GetFormatString(@dec decimal(28,8), @n int)
returns varchar(32) as
begin
declare @str varchar(32), @len int, @left varchar(32), @right varchar(32),@end varchar(32)
if @n!='0'
BEGIN
set @str= round(@dec,@n)
select @left=left(@str,charindex('.',@str)-1),@len=len(@left)-2
while @len>1
begin
select @left=stuff(@left,@len,0,','), @len=@len-3
end
select @right=left(stuff(@str,1,charindex('.',@str),''), @n),@len=4
while @len <=len(@right)
begin
select @right=stuff(@right,@len,0,','), @len=@len+4
end
set @end= @left+'.'+@right
end
else
BEGIN
set @str= round(@dec,@n)
select @left=left(@str,charindex('.',@str)-1),@len=len(@left)-2
while @len>1
begin
select @left=stuff(@left,@len,0,','), @len=@len-3
end
select @right=left(stuff(@str,1,charindex('.',@str),''), @n),@len=4
while @len <=len(@right)
begin
select @right=stuff(@right,@len,0,','), @len=@len+4
end
set @end= @left
end
return @end
end
select dbo.GetFormatString(123645.7889, 2)+'±'+'MON'
union all select dbo.GetFormatString(123645.7889, 3)+'±'+'MON'
union all select dbo.GetFormatString(123645.7889, 4)+'±'+'MON'
declare @d float
set @d = cast(day(getdate()) as float)/7
select
case
when @d<1 then '第一周'
when @d>1 and @d<2 then '第二周'
when @d>2 and @d<3 then '第三周'
when @d>3 and @d<4 then '第四周'
else '第五周'
end
alter proc p_getwk
@year int,--输入显示的年份
@month int --输入显示的月份
as
declare @d datetime
set @d=dateadd(wk,datediff(wk,'1900',cast(ltrim(@year*10000+@month*100+1) as datetime)),'1900')
;with t as
(
select top (datediff(dd,ltrim(@year*10000+@month*100+1),ltrim(@year*10000+(@month+1)*100+1)))
[date]=cast(ltrim(@year*10000+@month*100+1) as datetime)-1
+row_number()over(ORDER by getdate())
from sysobjects
)
select
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end AS 周数,
min([date]) 开始日期,
max([date]) 结束日期
from t
group by
case
when [date] between @d and @d+5 then '第一周'
when [date] between @d+6 and @d+12 then '第二周'
when [date] between @d+13 and @d+19 then '第三周'
when [date] between @d+20 and @d+26 then '第四周'
when [date] between @d+27 and @d+33 then '第五周'
else '第六周'
end
ORDER BY 2
GO
exec p_getwk 2010,5
/*
周数 开始日期 结束日期
------ ----------------------- -----------------------
第一周 2010-05-01 00:00:00.000 2010-05-01 00:00:00.000
第二周 2010-05-02 00:00:00.000 2010-05-08 00:00:00.000
第三周 2010-05-09 00:00:00.000 2010-05-15 00:00:00.000
第四周 2010-05-16 00:00:00.000 2010-05-22 00:00:00.000
第五周 2010-05-23 00:00:00.000 2010-05-29 00:00:00.000
第六周 2010-05-30 00:00:00.000 2010-05-31 00:00:00.000
(6 行受影响)
*/