create function [C6MartPeriodico].[get_appoDataAvvioMon]( @date datetime = null )
returns varchar(8)
as 
begin
if @date is null
set @date = getdate()
return cast(case when datepart(month, @date) = 1 then datepart(year,@date)  - 1 else datepart(year,@date)  end as varchar(4)) + 
case when datepart(month, @date) >= 11 then '12' when datepart(month, @date) >= 8 then '09' when datepart(month, @date) >= 5 then '06' when datepart(month, @date) > 1 then '03' else '12' end+
'01'
end