rezaei manesh
یک شنبه 06 اسفند 1385, 18:38 عصر
سلام
من باید جمعه های یک سال رو در اول هر سال در یک جدول بریزم
حالا می خوام یه پروسیجر بنویسم که این کار رو بکنه یعنی سال رو بگیره و جمعه های رو تولید
کنه با تاریخ فارسی و حدالمقدور با فرمت ماه و روز اگه کسی همچین کدی داره یه کمکی بکنه
البته خودم می تونم یه جورایی بنویسم اما هم وقت می بره و هم اون چیزی که تو سرمه زیاد خوب نیست
با sql 2000
darvishiali
یک شنبه 06 اسفند 1385, 22:59 عصر
سلام؛
فانکشن های زیر برای تبدیل تاریخ شمسی به میلادی و بالعکسه که برای کار شما لازمه.
CREATE FUNCTION fn_ShamsiDateStrPart (@ADateStr varchar(10), @ADatePart char)
RETURNS int
AS
BEGIN
declare @FY varchar(4), @FM varchar(2), @FD varchar(2),
@SlashPos1 int, @SlashPos2 int
if @ADateStr = '' return null
set @SlashPos1 = CHARINDEX('/', @ADateStr)
if (@SlashPos1 = 0) or ((@SlashPos1 <> 3) and (@SlashPos1 <> 5)) return null
set @SlashPos2 = CHARINDEX('/', @ADateStr, @SlashPos1 + 1)
if @SlashPos2 = 0 return null
set @FY = Cast(SUBSTRING (@ADateStr, 1 , @SlashPos1 - 1 ) as int)
set @FM = Cast(SUBSTRING (@ADateStr, @SlashPos1 + 1, @SlashPos2 - @SlashPos1 - 1) as int)
set @FD = Cast(SUBSTRING (@ADateStr, @SlashPos2 + 1, LEN(@ADateStr) - @SlashPos2) as int)
return
case @ADatePart
when 'Y' then @FY
when 'M' then @FM
when 'D' then @FD
end
END
CREATE function fn_DateToShamsiDatePart (@MiDate DateTime , @ADatePart char)
returns int
AS
begin
Declare @TmpY int, @Leap int
Declare @Sh_Y int , @Sh_M int , @Sh_D int, @Result int
if @MiDate is null
return 0
--Declare @Result int
Set @Result = convert(int, convert(float,@MiDate))
if @Result <= 78
begin
Set @Sh_Y = 1278
Set @Sh_M = (@Result + 10) / 30 + 10
Set @Sh_D = (@Result + 10) % 30 + 1
end
else
begin
Set @Result = @Result - 78
Set @Sh_Y = 1279
while 1 = 1
begin
Set @TmpY = @Sh_Y + 11
Set @TmpY = @TmpY - ( @TmpY / 33) * 33
if (@TmpY <> 32) and ( (@TmpY / 4) * 4 = @TmpY )
Set @Leap = 1
else
Set @Leap = 0
if @Result <= (365+@Leap)
break
Set @Result = @Result - (365+@Leap)
Set @Sh_Y = @Sh_Y + 1
end
if @Result <= 31*6
begin
Set @Sh_M = (@Result-1) / 31 + 1
Set @Sh_D = (@Result-1) % 31 + 1
end
else
begin
Set @Sh_M = ((@Result-1) - 31*6) / 30 + 7
Set @Sh_D = ((@Result-1) - 31*6) % 30 + 1
end
end
return
case @ADatePart
when 'Y' then @Sh_Y
when 'M' then @Sh_M
when 'D' then @Sh_D
else 0
end
end
CREATE function fn_DateToShamsiDate(@ChirsDate SmallDateTime)
returns Char(10)
as
begin
declare @SolarDate char(10)
declare @Day Char(2)
declare @Mon Char(2)
declare @SDay Int
declare @SMon Int
declare @SYear Int
set @SYear = dbo.fn_DateToShamsiDatePart(@ChirsDate, 'Y')
set @SMon = dbo.fn_DateToShamsiDatePart(@ChirsDate, 'M')
set @SDay = dbo.fn_DateToShamsiDatePart(@ChirsDate, 'D')
if @SMon <= 9
select @Mon = '0'+Convert(Char(1),@SMon)
else
select @Mon = Convert(Char(2),@SMon)
if @SDay <= 9
select @Day = '0'+Convert(Char(1),@SDay)
else
select @Day = Convert(Char(2),@SDay)
--تاریخ 8 کاراکتری
-- select @SolarDate = SubString(Convert(Char(4),@SYear),3,2)+'/'+@Mon+'/'+@Day
select @SolarDate = Convert(Char(4),@SYear)+'/'+@Mon+'/'+@Day
return @SolarDate
end
CREATE function fn_ShamsiStrDateToDate (@ShamsiDateStr char(10))
returns datetime
AS
begin
declare @Sh_Y int,
@Sh_M int,
@Sh_D int
SET @Sh_Y=dbo.fn_ShamsiDateStrPart(@ShamsiDateStr, 'Y')
SET @Sh_M=dbo.fn_ShamsiDateStrPart(@ShamsiDateStr, 'M')
SET @Sh_D=dbo.fn_ShamsiDateStrPart(@ShamsiDateStr, 'D')
declare @I int, @TmpY int, @Leap int, @D_of_Y int
Declare @Result DateTime
if @Sh_Y < 100
Set @Sh_Y = @Sh_Y + 1300
if @Sh_M >= 7
Set @D_of_Y = 31 * 6 + (@Sh_M-7) * 30 + @Sh_D
else
Set @D_of_Y = (@Sh_M-1) * 31 + @Sh_D
if @Sh_Y = 1278
begin
Set @Result = @D_of_Y-(31*6 + 3*30+11)
end
else
begin
Set @Result = 365 - (31*6 + 3*30+11) + 1
Set @I = 1279
while @I < @Sh_Y
begin
Set @TmpY = @I + 11
Set @TmpY = @TmpY - ( @TmpY / 33) * 33
if (@TmpY <> 32) and ( (@TmpY / 4) * 4 = @TmpY )
Set @Leap = 1
else
Set @Leap = 0
if @Leap = 1
Set @Result = @Result + 366
else
Set @Result = @Result + 365
Set @I = @I + 1
end
Set @Result = @Result + @D_of_Y - 1
end
return @Result
end
حالا کد زیر رو توی یه SP بنویس و بعد از شرطش کارهایی رو که میخوای انجام بده:
DECLARE @BEGINDATE DATETIME,
@ENDDATE DATETIME
SET @BEGINDATE=DBO.FN_SHAMSISTRDATETODATE('1385/01/01')
SET @ENDDATE=DBO.FN_SHAMSISTRDATETODATE('1385/12/29')
WHILE @BEGINDATE<=@ENDDATE
BEGIN
IF DATEPART(WEEKDAY, @BEGINDATE)=6
--روز جمعه است؛ عمل مربوطه انجام شود
PRINT DBO.FN_DATETOSHAMSIDATE(@BEGINDATE)+' IS FRIDAY'
SET @BEGINDATE=DATEADD(DAY, 1, @BEGINDATE)
END
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.