Hi everyone,
I have a function in VBA, which I would like to convert into Stored procedure in SQL -Server. I think I have most of it covered..Its just a For NExt loop which is completely wrong in my stored procedure. In fact I am not sure I can convert it like I am trying. I am a complete beginner in stored procedure , and hence appreciate any help.Thanks in Advance..Here is first of all the VBA function:
Private Function Sub_Unearned(D_Start As Date, D_End As Date) As Double
Dim No_Years As Long
Dim i As Long
Dim DaysPerYear As Integer
Dim Date_1 As Date
Dim Date_2 As Date
Sub_Unearned = 0
No_Years = Year(D_End) - Year(D_Start) + 1
For i = 1 To No_Years
DaysPerYear = 1 + DateSerial(Year(D_Start) + i - 1, 12, 31) - DateSerial(Year(D_Start) + i - 1, 1, 1)
Select Case i
Case 1
Date_1 = DateSerial(Year(D_Start), 12, 31)
Date_2 = D_Start
Case No_Years
Date_1 = D_End
Date_2 = DateSerial(Year(D_Start) + i - 1, 1, 1)
Case Else
Date_1 = DateSerial(Year(D_Start) + i - 1, 12, 31)
Date_2 = DateSerial(Year(D_Start) + i - 1, 1, 1)
End Select
If No_Years = 1 Then
Sub_Unearned = (D_End - D_Start + 1) / DaysPerYear
Else
Sub_Unearned = Sub_Unearned + (Date_1 - Date_2 + 1) / DaysPerYear
End If
Next i
End Function
and my attempted solution reads as follows:
CREATE FUNCTION dbo.fn_Sub_Unearned
(@D_Start datetime,@D_End datetime)
RETURNS numeric(7,0)
AS
BEGIN
declare @No_Years numeric(7,0)
declare @i numeric(7,0)
declare @Days_per_year int
declare @Date_1 as datetime
declare @Date_2 as datetime
declare @Rueckwert as numeric(7,0)
declare @MyCursor CURSOR
set @Rueckwert=0
set @No_Years=datename(year,@D_end)-datename(year,@D_start)+1
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
while @@fetch_status<@No_years
begin
@Days_per_year = 1 + datediff((datename(year,@D_Start)+@i-1,'1231'),(datename(year,@D_Start)+@i-1+'0101'))
select case @i
when 1 then @Date_1=datename(year,@D_Start)+'1231'
and @Date_2= @D_Start
when @No_Years then @Date_1 =@D_End
and @Date_2 =Datename(year,@D_Start)+@i-1+'0101'
else
@Date_1=Datename(year,@D_Start)+@i-1+'1231'
@Date_2=Datename(year,@D_Start)+@i-1+'0101'
end
if @No_Years =1 then
@rueckwert = (@D_End - @D_Start+1)/@Daysperyear
else
@rueckwert = @rueckwert + (@Date_1 - @Date_2 + 1) /@Daysperyear
end if
FETCH NEXT FROM @MyCursor
return @rueckwert
END
How can I convert the loop accordingly.??
Kind regards,
Kingsley
I have a function in VBA, which I would like to convert into Stored procedure in SQL -Server. I think I have most of it covered..Its just a For NExt loop which is completely wrong in my stored procedure. In fact I am not sure I can convert it like I am trying. I am a complete beginner in stored procedure , and hence appreciate any help.Thanks in Advance..Here is first of all the VBA function:
Private Function Sub_Unearned(D_Start As Date, D_End As Date) As Double
Dim No_Years As Long
Dim i As Long
Dim DaysPerYear As Integer
Dim Date_1 As Date
Dim Date_2 As Date
Sub_Unearned = 0
No_Years = Year(D_End) - Year(D_Start) + 1
For i = 1 To No_Years
DaysPerYear = 1 + DateSerial(Year(D_Start) + i - 1, 12, 31) - DateSerial(Year(D_Start) + i - 1, 1, 1)
Select Case i
Case 1
Date_1 = DateSerial(Year(D_Start), 12, 31)
Date_2 = D_Start
Case No_Years
Date_1 = D_End
Date_2 = DateSerial(Year(D_Start) + i - 1, 1, 1)
Case Else
Date_1 = DateSerial(Year(D_Start) + i - 1, 12, 31)
Date_2 = DateSerial(Year(D_Start) + i - 1, 1, 1)
End Select
If No_Years = 1 Then
Sub_Unearned = (D_End - D_Start + 1) / DaysPerYear
Else
Sub_Unearned = Sub_Unearned + (Date_1 - Date_2 + 1) / DaysPerYear
End If
Next i
End Function
and my attempted solution reads as follows:
CREATE FUNCTION dbo.fn_Sub_Unearned
(@D_Start datetime,@D_End datetime)
RETURNS numeric(7,0)
AS
BEGIN
declare @No_Years numeric(7,0)
declare @i numeric(7,0)
declare @Days_per_year int
declare @Date_1 as datetime
declare @Date_2 as datetime
declare @Rueckwert as numeric(7,0)
declare @MyCursor CURSOR
set @Rueckwert=0
set @No_Years=datename(year,@D_end)-datename(year,@D_start)+1
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
while @@fetch_status<@No_years
begin
@Days_per_year = 1 + datediff((datename(year,@D_Start)+@i-1,'1231'),(datename(year,@D_Start)+@i-1+'0101'))
select case @i
when 1 then @Date_1=datename(year,@D_Start)+'1231'
and @Date_2= @D_Start
when @No_Years then @Date_1 =@D_End
and @Date_2 =Datename(year,@D_Start)+@i-1+'0101'
else
@Date_1=Datename(year,@D_Start)+@i-1+'1231'
@Date_2=Datename(year,@D_Start)+@i-1+'0101'
end
if @No_Years =1 then
@rueckwert = (@D_End - @D_Start+1)/@Daysperyear
else
@rueckwert = @rueckwert + (@Date_1 - @Date_2 + 1) /@Daysperyear
end if
FETCH NEXT FROM @MyCursor
return @rueckwert
END
How can I convert the loop accordingly.??
Kind regards,
Kingsley