SQLServer 2005,SQLServer 2008,SQLServer 2014,SQLServer 2017
均可使用
alter function dbo.date_calc
(
@date date,
@para varchar(64) --月初 month_begin,月末 month_end,年初 year_begin,年末 year_end,季初 quarter_begin,季末 quarter_end
)
returns datetime
as
begin
set @date=
case
when @para='month_begin' then convert(varchar(8),@date,120)+'1' --月初
when @para='month_end' then dateadd(day,-1,convert(varchar(8),dateadd(m,1,@date),120)+'1 00:00:00') --月初
when @para='year_begin' then convert(char(4),year(@date))+'0101' --年初
when @para='year_end' then convert(char(4),year(@date))+'1231' --年底
when @para='quarter_begin' then dateadd(quarter,1,DATEADD(quarter, DATEDIFF(quarter,0,getdate())-1, 0)) --季初
when @para='quarter_end' then dateadd(quarter,1,DATEADD(quarter, DATEDIFF(quarter,0,getdate()), -1)) --季末
end
return @date
end
[Python,SQL,ETL]交流群 164142295