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 备注:本文的语法讲解确实是比较乱,似乎格式不太严谨.参考时还是以实例验证为准比较好 以下的文章主要 ...
随机推荐
- IPv6笔记
单播地址包含的几种类型:1.AGUA地址:2000::到3FFF:FFFF:FFFF:FFFF:FFFF..... 一个接口可以拥有若干个AGUA地址2.Link-Local 地址:FE80::/10 ...
- [JavaScript] 前端模块加载简单实现(require)
模块加载的简单实现 (function(win) { var baseUrl; var paths; var script_cache = {}; var script_queue = []; var ...
- c# EF code First生成数据库以及表
1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...
- TensorFlow(2)Softmax Regression
Softmax Regression Chapter Basics generate random Tensors Three usual activation function in Neural ...
- 第一册:lesson thirty one。
原文:Where is Sally? A:Where is .. B? B:She is in the garden,A. A:What's she doing? B:She is sitting u ...
- Base64字符保存图片,图片转换成Base64字符编码
//文件转换成Base64编码 public static String getFileBase64Str(String filePath) throws IOException { String f ...
- JSJ—编译器与虚拟机哪个重要?
阅读本文约“2分钟” 熟悉Java的朋友都知道虚拟机还有编译器,那么它们各自主要的功能是什么?谁比较重要呢?让我们来了解一下这两位美女的故事. 虚拟机可以说就是Java,她能让程序运行起来. 但是编译 ...
- 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)
官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...
- 教你搞定ElasticSearch(head)
简介: ElasticSearch(以下简称ES)是一个基于Lucene构建的开源(open-source),分布式(distributed),RESTful,实时(real-time)的搜索与分析( ...
- js之单例模式
单例模式是指一个类,只有一个实例.实现的思路是,创建实例时候加判断,如果有实例则返回,如果没有就new一个,并返回. 第一步: 创建类. function Waiter(id, name, salar ...