欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取指定日期所属年份的第一天日期或最后一天日期

程序员文章站 2022-07-07 19:38:27
写了2个自定义函数,获取指定日期所在年份的第一天日期或最后一天的日期: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- -- Author: Insus.NET -- Create date: 2019-05-09 -- Update date ......

写了2个自定义函数,获取指定日期所在年份的第一天日期或最后一天的日期:

 

set ansi_nulls on
go
set quoted_identifier on
go
-- =============================================
-- author:      insus.net
-- create date: 2019-05-09
-- update date: 2019-05-09
-- description: 获取指定日期所属年份的第一天日期
-- =============================================
create function [dbo].[svf_firstdateoftheyear]
( 
    @theyear datetime 
)
returns datetime
as
begin
    return  dateadd(year,datediff(year,0,@theyear),0)  
end

go

 

 

set ansi_nulls on
go
set quoted_identifier on
go
-- =============================================
-- author:      insus.net
-- create date: 2019-05-09
-- update date: 2019-05-09
-- description: 获取指定日期所属年份的最后一天日期
-- =============================================
create function [dbo].[svf_lastdateoftheyear]
( 
    @theyear datetime 
)
returns datetime
as
begin
    return dateadd(day,-1,dateadd(year,datediff(year,0,@theyear) + 1,0))
end
go

 

举例说明: