写了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 BEGINRETURN DATEADD(YEAR,DATEDIFF(YEAR,0,@TheYear),0) ENDGO
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 BEGINRETURN DATEADD(DAY,-1,DATEADD(YEAR,DATEDIFF(YEAR,0,@TheYear) + 1,0)) END GO
举例说明: