获取某年某月的第一天和最后一天的Sql Server函数
生活随笔
收集整理的這篇文章主要介紹了
获取某年某月的第一天和最后一天的Sql Server函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
留個底:
SET?ANSI_NULLS?ON
GO
SET?QUOTED_IDENTIFIER?ON
GO
--?=============================================
--?Author:????????RickyLin
--?Create?date:?2007-11-27
--?Description:????獲取某年某月的第一天的日期
--?=============================================
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
--?=============================================
--?Author:????????RickyLin
--?Create?date:?2007-11-27
--?Description:????獲取某年某月的最后一天的日期
--?=============================================
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
SET?ANSI_NULLS?ON
GO
SET?QUOTED_IDENTIFIER?ON
GO
--?=============================================
--?Author:????????RickyLin
--?Create?date:?2007-11-27
--?Description:????獲取某年某月的第一天的日期
--?=============================================
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
--?=============================================
--?Author:????????RickyLin
--?Create?date:?2007-11-27
--?Description:????獲取某年某月的最后一天的日期
--?=============================================
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
轉載于:https://www.cnblogs.com/Ricky81317/archive/2007/11/27/974307.html
總結
以上是生活随笔為你收集整理的获取某年某月的第一天和最后一天的Sql Server函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ScottGu之博客翻译-LINQ to
- 下一篇: 使用 RUP 管理小型项目和团队