获取某年某月的第一天和最后一天的SqlServer函数
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
-
-
-
-
-
- ALTER FUNCTION fnGetFirstDayOfMonth
- (
- @Year SMALLINT
- , @Month TINYINT
- )
- RETURNS SMALLDATETIME
- AS
- BEGIN
- DECLARE @FirstDay SMALLDATETIME
- DECLARE @Str VARCHAR(10)
- SET @Str = Cast(@Year AS VARCHAR(4)) + '-' + Cast(@Month AS VARCHAR(2)) + '-1'
-
- SET @FirstDay = Convert(SMALLDATETIME, @Str)
-
- RETURN @FirstDay
-
- END
- GO
-
-
-
-
-
-
- CREATE FUNCTION fnGetLastDayOfMonth
- (
- @Year SMALLINT
- , @Month TINYINT
- )
- RETURNS SMALLDATETIME
- AS
- BEGIN
- DECLARE @LastDay SMALLDATETIME
- DECLARE @Str VARCHAR(10)
- SET @Str = Cast(@Year AS VARCHAR(4)) + '-' + Cast(@Month AS VARCHAR(2)) + '-1'
-
- SET @LastDay = DateAdd(d, -1, DateAdd(m, 1, dbo.fnGetFirstDayOfMonth(@Year, @Month)))
-
- RETURN @LastDay
-
- END
- GO