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 备注:本文的语法讲解确实是比较乱,似乎格式不太严谨.参考时还是以实例验证为准比较好 以下的文章主要 ...
随机推荐
- .NET ThreadPool算法
.NET ThreadPool相关算法记录 1.ManagedThreadPool (corefx) .net4.0之前只有全局队列,为了解决全局队列多线程竞争使用问题,引入work-stealing ...
- Mysql加锁过程详解(6)-数据库隔离级别(1)
Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...
- Spring Boot中如何扩展XML请求和响应的支持
在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...
- Java核心技术 对象与类
对象与对象变量: 要想使用对象,就必须首先构造对象,并指定其初始状态,然后,对对象应用方法. 在Java中,使用构造器构造新实例.构造器是一种特殊的方法,用来构造并初始化对象. 在实际开发中,通常需要 ...
- 【golang-GUI开发】QSS的使用(一)———QSS入门指南
在这篇文章中我们将初步体验对qss的使用.并对在goqt中使用qss时的注意事项进行说明. 那么事不宜迟,现在开始我们的qss之旅吧. QSS语法入门 qss是一种与css3相似的控制Qt组件的样式表 ...
- 快乐的一天从JAVA第一课开始,生活美滋滋!!!
---恢复内容开始--- 学JAVA第一天 今天稀里糊涂就把JAVA环境配好了 现在回想一下,吧环境跟大家分享一下…… 第一步:下载 JAVA(推荐使用谷歌浏览器,因为谷歌浏览器右键点 ...
- Java 学习笔记 反射与迭代器
反射 使用反射获得类 Class cls = Class.forName("全类名") //包括包名 Class cls = xx.Class;//xx代表类名 使用反射获得构造方 ...
- Flask 系列之 HelloWorld
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 创建一个显示 Hello World 页面的网站. 环境 ...
- win7卸载JDK出现windows Installer程序包有问题
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://mp.csdn.net/postedit/82387967 操作系统:Windows 7 (64位) jdk版本:jdk1 ...
- localStorage封装借口store.js的使用
localstorage 是 HTML5 提供的在客户端存储数据的新方法,主要作用是将数据保存在客户端中,并且数据是永久保存的,除非人为干预删除. localstorage 的局限 1.只有版本较高的 ...