set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO create FUNCTION [dbo].[FunSunDate]( @DS datetime ) RETURNS char(8) AS begin if @ds is null set @ds=getdate() declare @YY int declare @MM int declare @DD int declare @OrginalDate datetime declare @CurrentDate datetime declare @InterVal int declare @Kabiseh int Declare @ConvertedDate char(8) set @kabiseh=0 set @YY = Year(@DS) set @MM = Month(@DS) set @DD = Day(@DS) set @CurrentDate = convert(datetime,str( @mm) + '/' + str( @dd)+ '/' + str( @YY) ,101) set @OrginalDate = convert(datetime,'01/01/'+str( @YY) ,101 ) set @DD = DateDiff(day, @OrginalDate, @CurrentDate) set @YY = @YY - 1900 set @InterVal = 287 If (((@YY - 1) % 4) = 0) --eng kabiseh set @Kabiseh = 1 set @DD = @DD + @InterVal + @Kabiseh set @YY = @YY - 22 set @DD = @DD - 1 --baraye inkeh rooz 30 v 31 ham dashteh bashim If @DD > 365 begin set @DD = @DD - 365 - @Kabiseh set @YY = @YY + 1 end If @DD >= 186 begin set @MM = 6 set @DD = @DD - 186 set @MM = @MM + floor((@DD / 30)) + 1 set @DD = (@DD % 30) end Else BEGIN set @MM = floor((@DD / 31) ) + 1 set @DD = (@DD % 31) End set @DD = @DD + 1 set @ConvertedDate= str( @YY,2) + '/' + right('00' + ltrim(str( @mm)),2) + '/' + right('00' + ltrim(str( @dd)),2) return @ConvertedDate end