首页 > 代码库 > 获取指定日期的常用前后节点(月初月末周一周末等等)

获取指定日期的常用前后节点(月初月末周一周末等等)

原文:获取指定日期的常用前后节点(月初月末周一周末等等)

注:周节点方面是根据中国习惯,视周一为起,周日为末。

/*---------------------------------函数:获取某日期的特定起止节点v0.01Author:AhDungUpdate:201305151755---------------------------------*/ALTER FUNCTION dbo.FGetSpecialDate_ahdung(@date DATE, @SpcDate VARCHAR(20))RETURNS DATE ASBEGINIF @SpcDate IN (YearBeg,YB,NC,年初) RETURN DATENAME(YEAR,@date)+-01-01ELSE IF @SpcDate IN (YearEnd,YE,NW,年尾,年末) RETURN DATENAME(YEAR,@date)+-12-31ELSE IF @SpcDate IN (QuarterBeg,QtBeg,QB,JC,季初) RETURN DATENAME(YEAR,@date)+CASE WHEN MONTH(@date)<=3 THEN -01 WHEN MONTH(@date)<=6 THEN -04 WHEN MONTH(@date)<=9 THEN -07 ELSE -10 END +-01ELSE IF @SpcDate IN (QuarterEnd,QtEnd,QE,JW,季尾,季末) RETURN DATENAME(YEAR,@date)+CASE WHEN MONTH(@date)<=3 THEN -03-31 WHEN MONTH(@date)<=6 THEN -06-30 WHEN MONTH(@date)<=9 THEN -09-30 ELSE -12-31 ENDELSE IF @SpcDate IN (MonBeg,MB,YC,月初) RETURN CONVERT(CHAR(7),@date,120)+-01ELSE IF @SpcDate IN (MonEnd,ME,YW,月尾,月末) RETURN DATEADD(DAY,-1,CONVERT(CHAR(7),DATEADD(MONTH,1,@date),120)+-01)ELSE IF @SpcDate IN (WeekBeg,WB,ZY,周一) RETURN DATEADD(DAY,2-CASE DATEPART(dw,@date) WHEN 1 THEN 8 ELSE DATEPART(dw,@date) END,@date)ELSE IF @SpcDate IN (WeekEnd,WE,ZR,ZM,周日,周末) RETURN DATEADD(DAY,8-CASE DATEPART(dw,@date) WHEN 1 THEN 8 ELSE DATEPART(dw,@date) END,@date)RETURN @dateEND

 

获取指定日期的常用前后节点(月初月末周一周末等等)