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

写了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
Source Code

 

 

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
Source Code

 

举例说明:

 

posted @ 2019-05-09 16:51  Insus.NET  阅读(1908)  评论(0编辑  收藏  举报