USE [自己的数据库名] GO /****** Object: UserDefinedFunction [dbo].[GetMonthDayNum] Script Date: 2020/12/9 13:07:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /*========================================================= 创建人:LJF 描 述:获取输入日期当月的天数。 测 试:SELECT [dbo].[GetMonthDayNum]('2020-11-01') =========================================================*/ CREATE FUNCTION [dbo].[GetMonthDayNum] ( @when DATETIME ) RETURNS INT AS BEGIN DECLARE @rv INT; IF DATEPART(MONTH, @when) = 2 BEGIN IF DATEPART(YEAR, @when) % 400 = 0 SELECT @rv = 29; ELSE IF DATEPART(YEAR, @when) % 100 = 0 SELECT @rv = 28; ELSE IF DATEPART(YEAR, @when) % 4 = 0 SELECT @rv = 29; ELSE SELECT @rv = 28; END; -- if ELSE BEGIN SELECT @rv = CASE (DATEPART(MONTH, @when)) WHEN 4 THEN 30 WHEN 6 THEN 30 WHEN 9 THEN 30 WHEN 11 THEN 30 ELSE 31 END; -- case END; -- else RETURN @rv; END; -- fn def