sql生成连续日期(年份、月份、日期)

此随笔主在分享日常可能用到的sql函数,用于生成连续日期(年份、月份、日期)

具体的看代码及效果吧!

1-- ============================================= 2-- Author: <Author,Jearay> 3-- Create date: <Create Date,2018/7/12> 4-- Description: <Description,返回连续日期(年份或月份或日期)> 5-- ============================================= 6CREATE FUNCTION [dbo].[fn_GetContinuousDate] 7( 8 @date datetime, --基准日期 9 @type nvarchar(10),--'year、y','month、mon、m','day、d','yearmonth、ym','monthday、md' 10 @prev int, --往前数量 11 @next int --后续数量 12) 13RETURNS 14 @return TABLE 15( 16 DataDate date,DateAlis nvarchar(20),DateCommon nvarchar(20) 17) 18AS 19BEGIN 20 declare @tempDate date,@tempDateAlis nvarchar(20),@tempDateCommon nvarchar(20),@index int=1 21 --年份 22 if LOWER(@type)=N'year' or LOWER(@type)=N'y' 23 begin 24 set @date=dateadd(year,DATEDIFF(year,0,@date),0) 25 --写入往前数量的年份 26 while @prev>0 27 begin 28 set @tempDate=dateadd(year,-@prev,@date) 29 insert @return 30 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) 31 set @prev=@prev-1 32 end 33 --写入当年 34 insert @return 35 select @date,cast(year(@date) as nvarchar(4))+N'年',cast(year(@date) as nvarchar(4)) 36 --写入后续数量的年份 37 while @next-@index>=0 38 begin 39 set @tempDate=dateadd(year,@index,@date) 40 insert @return 41 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) 42 set @index=@index+1 43 end 44 45 end 46 --月份 47 else if LOWER(@type)=N'month' or LOWER(@type)=N'm' or LOWER(@type)=N'mon' 48 begin 49 set @date=dateadd(month,DATEDIFF(month,0,@date),0) 50 --写入往前数量的月份 51 while @prev>0 52 begin 53 set @tempDate=dateadd(month,-@prev,@date) 54 insert @return 55 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月',cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 56 set @prev=@prev-1 57 end 58 --写入当月 59 insert @return 60 select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月',cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2)) 61 --写入后续数量的月份 62 while @next-@index>=0 63 begin 64 set @tempDate=dateadd(month,@index,@date) 65 insert @return 66 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月',cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 67 set @index=@index+1 68 end 69 70 end 71 --日期 72 else if LOWER(@type)=N'day' or LOWER(@type)=N'd' 73 begin 74 set @date=dateadd(day,DATEDIFF(day,0,@date),0) 75 --写入往前数量的日期 76 while @prev>0 77 begin 78 set @tempDate=dateadd(day,-@prev,@date) 79 insert @return 80 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 81 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) 82 set @prev=@prev-1 83 end 84 --写入当日 85 insert @return 86 select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月'+cast(day(@date) as nvarchar(2))+N'日' 87 ,cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2))+N'/'+cast(day(@date) as nvarchar(2)) 88 --写入后续数量的日期 89 while @next-@index>=0 90 begin 91 set @tempDate=dateadd(day,@index,@date) 92 insert @return 93 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 94 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) 95 set @index=@index+1 96 end 97 98 end 99 --年中月 100 else if LOWER(@type)=N'yearmonth' or LOWER(@type)=N'ym' 101 begin 102 set @date=dateadd(year,DATEDIFF(year,0,@date),0) 103 set @index=0 104 --写入年对应月份 105 while 12-@index>0 106 begin 107 set @tempDate=dateadd(month,@index,@date) 108 insert @return 109 select @tempDate,cast(month(@tempDate) as nvarchar(2))+N'月' 110 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 111 set @index=@index+1 112 end 113 end 114 --月中日, 分自然月和指定月 115 else if LOWER(@type)=N'monthday' or LOWER(@type)=N'md' 116 begin 117 --指定月 118 --指定月开始日期、结束日期 119 if @prev>0 and @next>0 120 begin 121 declare @endDate date 122 set @date=dateadd(month,DATEDIFF(month,0,@date),0) --获取月份 123 set @endDate=dateadd(day,@next,@date) 124 set @index=datediff(day,@endDate,dateadd(day,@prev-1,dateadd(month,-1,@date))) 125 --写入月对应日期 126 while @index<0 127 begin 128 set @tempDate=dateadd(day,@index,@endDate) 129 insert @return 130 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 131 ,@tempDate 132 set @index=@index+1 133 end 134 end 135 --自然月 136 else 137 begin 138 set @date=dateadd(month,DATEDIFF(month,0,@date),0) 139 set @index=datediff(day,dateadd(month,1,@date),@date) 140 set @date=dateadd(month,1,@date) 141 --写入月对应日期 142 while @index<0 143 begin 144 set @tempDate=dateadd(day,@index,@date) 145 insert @return 146 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 147 ,@tempDate 148 set @index=@index+1 149 end 150 end 151 152 153 end 154 RETURN 155END

View Code

函数调用示例:

1--返回今天往前3天至今天往后2天的连续日期 2select * from dbo.fn_GetContinuousDate(getdate(),'d',3,2)

结果如下:

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用