首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>数据库>基础常识>sql语句实现根据月分打印月历的两种方案
sql语句实现根据月分打印月历的两种方案
来源: 发布时间:2007-12-19 发布人: 浏览: 人次   字体: [ ]  
第一种方案:
create function fn_Calendar(@year int@month int)
returns nvarchar(max)
as
begin
    
declare @result nvarchar(max), @Enter nvarchar(8)
    
select @Enter = char(13)+char(10),  @result = ' Sun Mon The Wed Thu Fri Sta' + @Enter --表头

    
declare @start datetime@end datetime
    
select @start = rtrim(@year)+'-'+rtrim(@month)+'-1'@end = dateadd(mm, 1@start)    

    
set @result = @result+replicate('    ', (datepart(dw, @start)+@@datefirst+6)%7)    --第一行前面的空格
    while datediff(d, @start@end)>0
    
begin
        
if (datepart(dw, @start)+@@datefirst)%7 = 1
            
select @result = @result+@Enter --是否换行
        select @result = @result+right('   '+rtrim(day(@start)), 4), @start = dateadd(d, 1@start)
    
end
    
return @result
end
go

set datefirst 3 
print dbo.fn_Calendar(200712)
select dbo.fn_Calendar(200712)
set datefirst 7

drop function dbo.fn_Calendar

/*
 Sun Mon The Wed Thu Fri Sta
                           1
   2   3   4   5   6   7   8
   9  10  11  12  13  14  15
  16  17  18  19  20  21  22
  23  24  25  26  27  28  29
  30  31

------------------------------------------
 Sun Mon The Wed Thu Fri Sta
                           1
   2   3   4   5   6   7   8
   9  10  11  12  13  14  15
  16  17  18  19  20  21  22
  23  24  25  26  27  28  29
  30  31

(1 row(s) affected)
*/

 

第二种方案: 

create function f_calendar(@year int,@month int)
returns @t table(日 varchar(4),一 varchar(4),二 varchar(4),三 varchar(4),四 varchar(4),五 varchar(4),六 varchar(4))
as
begin

    
declare @a table(id int identity(0,1),date datetime)
    
    
insert into @a(date) 
    
select top 31 rtrim(@year)+'-'+rtrim(@month)+'-1' from sysobjects
    
    
update @a set date=dateadd(dd,id,date)    

    
insert into @t
    
select
        
max(case datepart(dw,date) when 7 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 1 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 2 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 3 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 4 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 5 then rtrim(day(date)) else '' end),
        
max(case datepart(dw,date) when 6 then rtrim(day(date)) else '' end)
    
from
        
@a
    
where
        
month(date)=@month
    
group by
        (
case datepart(dw,date) when 7 then datepart(week,date)+1 else datepart(week,date) end)

    
return
end
go

set datefirst 1
select * from dbo.f_calendar(2007,12)
/*
日    一    二    三   四    五    六    
---- ---- ---- ---- ---- ---- ---- 
                              1
2    3    4    5    6    7    8
9    10   11   12   13   14   15
16   17   18   19   20   21   22
23   24   25   26   27   28   29
30   31                  
*/

go

drop function f_calendar
go

 

 对比一下,感觉第一种的更容易理解,而且不管@@datefirst的值怎么变化都不会出错,第二种方案的需要手动设置(set datefirst 1)。 另外,第一种方案是直接返回一个串,第二种方案返回的是一个table。


相 关 文 章   发布商链接
·设计一个用户,角色,功能三者之间的...
·SQL语法中常用字符串函数大全
·DB2、oralce、mysql数据库取前十条记...
·祥解磁盘 I/O 性能 对数据库的影响
·SQL中IN,NOTIN,EXISTS,NOT EXISTS的...
·软件开发之数据库设计技巧大放送
·速学数据库链路的建立及其使用
·如何查找数据库中某一字段所在的表名...
·两个实现删除数据库字段中汉字或字符...
·sql语句获取每月最后一天示例
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·sql语句实现行数据以列等式进
·sql语句获取每月最后一天示例
·两个实现删除数据库字段中汉...
·如何查找数据库中某一字段所...
·速学数据库链路的建立及其使...
·软件开发之数据库设计技巧大...
·SQL中IN,NOTIN,EXISTS,NOT EX...
·DB2、oralce、mysql数据库取...
·SQL语法中常用字符串函数大全
·设计一个用户,角色,功能三...
·SQL数据库面试题目及其答案
·SET IDENTITY_INSERT的用法
·sql语句使用内嵌视图与临时表
热 门 文 章
·SQL数据库面试题目及其答案
·SQL中IN,NOTIN,EXISTS,NOT EX...
·SQL语法中常用字符串函数大全
·SET IDENTITY_INSERT的用法
·SQL查询指定条数的记录语句
·超全超强SQL语法大全
·祥解磁盘 I/O 性能 对数据库...
·DB2、oralce、mysql数据库取...
·sql语句获取每月最后一天示例
·速学数据库链路的建立及其使...
·两个实现删除数据库字段中汉...
·比较不错的SQL分页查询存储过...
·sql日期计算日期精确到天,包...
·软件开发之数据库设计技巧大...
·sql语句使用内嵌视图与临时表...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .