ufn_GetWeekFirstAndEndDay 获取指定日期所在星期的第一天和最后一天日期
ALTER FUNCTION [dbo].[ufn_GetWeekFirstAndEndDay](@tmpDate DATETIME)
RETURNS @tmpTable TABLE(
FirstDay DATETIME ,
EndDay DATETIME
)
AS
BEGIN
INSERT INTO @tmpTable
SELECT a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy
) a
LEFT JOIN (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay
) b
ON a.ID = b.ID
RETURN
END
ufn_GetMonthFirstAndEndDay 获取指定日期坐在月的第一天和最后一天
ALTER FUNCTION [dbo].[ufn_GetMonthFirstAndEndDay](@tmpDate DATETIME)
RETURNS @tmpTable TABLE(
FirstDay DATETIME ,
EndDay DATETIME
)
AS
BEGIN
INSERT INTO @tmpTable
SELECT a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID, CONVERT(varchar(10),DATEADD(DAY,-DATEPART(DAY,GETDATE())+1,GETDATE()),23) +' 00:00:00' AS 'FirstDay'
) a
LEFT JOIN (
SELECT 1 AS ID, CONVERT(varchar(10),DATEADD(DAY,-1,DATEADD(MONTH,1,GETDATE()-DAY(GETDATE())+1)),23)+' 23:59:59' AS 'EndDay'
) b
ON a.ID = b.ID
RETURN
END