sql生成连续日期(年份、月份、日期)
此随笔主在分享日常可能用到的sql函数,用于生成连续日期(年份、月份、日期)
具体的看代码及效果吧!
-- =============================================
-- Author: <Author,Jearay>
-- Create date: <Create Date,2018/7/12>
-- Description: <Description,返回连续日期(年份或月份或日期)>
-- =============================================
CREATE FUNCTION [dbo].[fn_GetContinuousDate]
(
@date datetime, --基准日期
@type nvarchar(10),--'year、y','month、mon、m','day、d','yearmonth、ym','monthday、md'
@prev int, --往前数量
@next int --后续数量
)
RETURNS
@return TABLE
(
DataDate date,DateAlis nvarchar(20),DateCommon nvarchar(20)
)
AS
BEGIN
declare @tempDate date,@tempDateAlis nvarchar(20),@tempDateCommon nvarchar(20),@index int=1
--年份
if LOWER(@type)=N'year' or LOWER(@type)=N'y'
begin
set @date=dateadd(year,DATEDIFF(year,0,@date),0)
--写入往前数量的年份
while @prev>0
begin
set @tempDate=dateadd(year,-@prev,@date)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4))
set @prev=@prev-1
end
--写入当年
insert @return
select @date,cast(year(@date) as nvarchar(4))+N'年',cast(year(@date) as nvarchar(4))
--写入后续数量的年份
while @next-@index>=0
begin
set @tempDate=dateadd(year,@index,@date)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4))
set @index=@index+1
end end
--月份
else if LOWER(@type)=N'month' or LOWER(@type)=N'm' or LOWER(@type)=N'mon'
begin
set @date=dateadd(month,DATEDIFF(month,0,@date),0)
--写入往前数量的月份
while @prev>0
begin
set @tempDate=dateadd(month,-@prev,@date)
insert @return
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))
set @prev=@prev-1
end
--写入当月
insert @return
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))
--写入后续数量的月份
while @next-@index>=0
begin
set @tempDate=dateadd(month,@index,@date)
insert @return
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))
set @index=@index+1
end end
--日期
else if LOWER(@type)=N'day' or LOWER(@type)=N'd'
begin
set @date=dateadd(day,DATEDIFF(day,0,@date),0)
--写入往前数量的日期
while @prev>0
begin
set @tempDate=dateadd(day,-@prev,@date)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日'
,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2))
set @prev=@prev-1
end
--写入当日
insert @return
select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月'+cast(day(@date) as nvarchar(2))+N'日'
,cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2))+N'/'+cast(day(@date) as nvarchar(2))
--写入后续数量的日期
while @next-@index>=0
begin
set @tempDate=dateadd(day,@index,@date)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日'
,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2))
set @index=@index+1
end end
--年中月
else if LOWER(@type)=N'yearmonth' or LOWER(@type)=N'ym'
begin
set @date=dateadd(year,DATEDIFF(year,0,@date),0)
set @index=0
--写入年对应月份
while 12-@index>0
begin
set @tempDate=dateadd(month,@index,@date)
insert @return
select @tempDate,cast(month(@tempDate) as nvarchar(2))+N'月'
,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))
set @index=@index+1
end
end
--月中日, 分自然月和指定月
else if LOWER(@type)=N'monthday' or LOWER(@type)=N'md'
begin
--指定月
--指定月开始日期、结束日期
if @prev>0 and @next>0
begin
declare @endDate date
set @date=dateadd(month,DATEDIFF(month,0,@date),0) --获取月份
set @endDate=dateadd(day,@next,@date)
set @index=datediff(day,@endDate,dateadd(day,@prev-1,dateadd(month,-1,@date)))
--写入月对应日期
while @index<0
begin
set @tempDate=dateadd(day,@index,@endDate)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日'
,@tempDate
set @index=@index+1
end
end
--自然月
else
begin
set @date=dateadd(month,DATEDIFF(month,0,@date),0)
set @index=datediff(day,dateadd(month,1,@date),@date)
set @date=dateadd(month,1,@date)
--写入月对应日期
while @index<0
begin
set @tempDate=dateadd(day,@index,@date)
insert @return
select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日'
,@tempDate
set @index=@index+1
end
end end
RETURN
END
函数调用示例:
--返回今天往前3天至今天往后2天的连续日期
select * from dbo.fn_GetContinuousDate(getdate(),'d',3,2)
结果如下:

sql生成连续日期(年份、月份、日期)的更多相关文章
- 用sql 生成2016年全年的日期
select to_char(日期,'yyyy-mm-dd') from( select to_date('2016-01-01','yyyy-mm-dd') + level 日期 from dual ...
- java 获取当前年份 月份 日期
import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar ...
- sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期
问题:在数据库脚本开发中,有时需要生成一堆连续数字或者日期,例如yearly report就需要连续数字做年份,例如daily report就需要生成一定时间范围内的每一天日期.而自带的系统表mast ...
- SQL 生成一个日期范围
有时想按日或月生成一个序列,就像2014-1-1.2014-1-2.2014-1-3... 在sql server中可以写个函数来实现. /* 生成一个日期范围,如2014.01.2014.02... ...
- PHP中查询指定时间范围内的所有日期,月份,季度,年份
/** * 查询指定时间范围内的所有日期,月份,季度,年份 * * @param $startDate 指定开始时间,Y-m-d格式 * @param $endDate 指定结束时间,Y-m-d格式 ...
- SQL Server中一些有用的日期sql语句
SQL Server中一些有用的日期sql语句 1.一个月第一天的 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 2.本周的星期一 SELECT DA ...
- Sql Server函数全解<四>日期和时间函数
原文:Sql Server函数全解<四>日期和时间函数 日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外, ...
- 【转】SQL SERVER 2005中如何获取日期(一个月的最后一日、上个月第一天、最后一天、一年的第一日等等)
在网上找到的一篇文章,相当不错哦O(∩_∩)O~ //C#本周第一天 int dayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek ...
- SQL Server使用convert对datetime日期数据进行获取
来源:http://database.51cto.com/art/201007/211883.htm 备注:本文的语法讲解确实是比较乱,似乎格式不太严谨.参考时还是以实例验证为准比较好 以下的文章主要 ...
随机推荐
- Go pprof性能监控
Go net/http/pprof包提供了一个在WEB项目中使用的性能监控的工具, 使用时只需要引用包: _"net/http/pprof" 然后就可以在浏览器中访问地址: htt ...
- Integer判等的陷阱:你知道Integer内部高速缓冲区IntegerCache吗?
https://blog.csdn.net/magician_Code/article/details/51469101 我们先来看看下面代码的运行情况: public static void mai ...
- SpringBoot系列——aop 面向切面
前言 项目中我们经常会用到aop切面,比如日志记录:这里简单记录一下springboot是如何使用aop spring对aop的配置,来自springboot参考手册,Common applicati ...
- c#中的特性Attribute
一:特性是什么?特性怎么创建怎么使用? 这一章节,我想谈谈c#的特性方面的知识,特性大家在工作开发中都很熟悉,比如我们经常见到的 1:key Display --EF 2:Import 3:HttpG ...
- 在go modules里使用go get进行包管理
上一篇文章里我们介绍了go modules的初步使用,现在我们来更深入的了解一下如何使用go get在module中管理依赖. module下的包管理 首先我们介绍过go mod edit修改go.m ...
- Linq中的Select与Select many
Select与Select Many 之前在项目中查询数据库中的数据,都是通过sql语句来查询的,但是随着时代的发展,微软在.Net Framework 4.5版中推出的一个主要的特性——LINQ. ...
- c# 画布验证码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [日常]nginx与网络事件模型
Nginx 的特点: 1.处理静态文件 2.反向代理加速 3.fastCGI,简单的负载均衡和容错 4.模块化的结构 5.分阶段资源分配技术,使得它的 CPU 与内存占用率非常低,保持 10,000 ...
- [android] xml文件的序列化
生成xml文件,模拟备份短信,创建短信的业务bean,创建一个domain的包放业务bean,这个业务bean里面,定义成员属性,生成get set方法,生成有参和无参的构造方法. 生成随机数,实例化 ...
- Runnable和Callable接口辨析
突然发现和启动一个线程有关的有三函数,run(), call(), start(),有点小乱,所以特别梳理一下 首先说一下start(),这个是最好说的,感觉start()和run()这俩名字是真的有 ...