تبدیل تاریخ میلادی به شمسی در Sql server

برای تبدیل تاریخ میتونید از استورپروسیجر زیر استفاده کنید (ادیتور پرشین بلاگ قابلیت هاش کم شده نتونستم کد رو درست وارد کنم):


Create Proc spConvertToFarsiDate

@EDate DateTime, 

@FDate Char(10) output

With Encryption

as

 Declare @EYear int, @EMon smallint, @EDay smallint, @ELeap bit, @EMonArray char(12), @EDayOfYear int

 Declare @FYear int, @FMon smallint, @FDay smallint, @FLeap bit, @FMonArray char(12)

 Select @FMonArray= char(31)+char(31)+char(31)+char(31)+char(31)+char(31)+char(30)+char(30)+char(30)+char(30)+char(30)+char(29)

 Select @EMonArray= char(31)+char(28)+char(31)+char(30)+char(31)+char(30)+char(31)+char(31)+char(30)+char(31)+char(30)+char(31)

 Select @EYear= Year(@EDate)

 Select @EMon= Month(@EDate)

 Select @EDay= Day(@EDate)

 if (@EYear %4)=0 Select @ELeap=1 else Select @ELeap=0

 --------------------- Calc Day Of Year

 Declare @Temp int, @Cnt int

 Select @Cnt=@EMon-1

 Select @Temp=0

 While @Cnt<>0 begin

  if (@Cnt=2)and(@ELeap=1) Select @Temp= @Temp+29

  else Select @Temp= @Temp + Ascii(Substring(@EMonArray, @Cnt, 1))

  Select @Cnt=@Cnt-1

 end

 Select @EDayOfYear= @Temp+@EDay

 ---------------------- Convert to Farsi

 Select @Temp= @EDayOfYear-79

 if @Temp>0 Select @FYear= @EYear-621

 else begin

 Select @FYear= @EYear-622

 if ((@FYear %4)=3) Select @Temp= @Temp+366 else Select @Temp= @Temp+365

 end

 if (@FYear %4)=3 Select @FLeap=1 else Select @Fleap=0

 Select @Cnt= 1

 While (@Temp<>0) and (@Temp>Ascii(Substring(@FMonArray, @Cnt, 1))) begin

  if @Cnt=12

   if (@FLeap=1) Select @Temp=@Temp-30 else Select @Temp= @Temp-29

  else Select @Temp= @Temp-Ascii(Substring(@FMonArray, @Cnt, 1))

  Select @Cnt= @Cnt+1

 end

 if @Temp<>0 begin

 Select @FMon= @Cnt

 Select @FDay= @Temp

 end else begin

 Select @FMon= 12

 Select @FDay=30

 end

 ------------------ CREATE Output

 Declare @YStr char(4), @MStr char(2), @DStr char(2)

 select @YStr= Convert(Char, @FYear)

 if @FMon<10 Select @MStr='0'+Convert(Char,@FMon) else Select @MStr=Convert(Char, @FMon)

 if @FDay<10 Select @DStr='0'+Convert(Char,@FDay) else Select @DStr=Convert(Char, @FDay)

 Select @FDate= @YStr+'/'+@MStr+'/'+@dStr

 ------------------ End Of PROCEDURE

Select @FDate = SubString(@FDate, 3 , 8)

GO

--*********************************************************************************************--

--*********************************************************************************************--



Create Proc spGetPersianDate

@pd varchar(10) output

With Encryption

as

declare @fd char(10)

declare @ed datetime

set @ed=getdate()

exec spConvertToFarsiDate @ed,@fd output

if getdate()>'2021/03/21' set @pd='14'+@fd else set @pd='13'+@fd

/ 0 نظر / 29 بازدید