该函数应在当前日期之后返回一个新日期,并添加num_of_flows。 num_of_flows是一个整数值
ALTER function [dbo].[M20_22_GetLoanMaturityDate_FIN_10]
(
@AcctAcid varchar(50),
@ScheduleNum varchar(50),
@bank_id varchar(6) = '03'
)
returns varchar(50)
as
BEGIN
Declare @lMaturityDate varchar(50)
begin
select @lMaturityDate =
case Finacle_Lrs.LR_FREQ_TYPE
when 'W' then dateadd(day, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 7,Finacle_Lrs.FLOW_START_DATE)
when 'F' then dateadd(day, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 14,Finacle_Lrs.FLOW_START_DATE)
when 'M' then (dateadd(month, Finacle_Lrs.NUM_OF_FLOWS - 1, Finacle_Lrs.FLOW_START_DATE))
when 'Q' then (dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 3, Finacle_Lrs.FLOW_START_DATE))
when 'H' then dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 6, Finacle_Lrs.FLOW_START_DATE)
when 'Y' then dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 12, Finacle_Lrs.FLOW_START_DATE)
end
from [TEST_EIMDW_Archive].[ARCHOWN].[FINCL10_LRS] Finacle_Lrs
where Finacle_Lrs.acid = @AcctAcid
and Finacle_Lrs.shdl_num = @ScheduleNum
and Finacle_Lrs.FLOW_ID in ('PRDEM','EIDEM')
AND Finacle_Lrs.ACTIVE_FLAG = 'Y' AND Finacle_Lrs.DELETE_FLAG= 'N'
and Finacle_Lrs.bank_id = @bank_id
and Finacle_Lrs.FLOW_START_DATE = ( select max(a.FLOW_START_DATE)
from [TEST_EIMDW_Archive].[ARCHOWN].[FINCL10_LRS] a
where a.ACID = Finacle_Lrs.ACID
and a.SHDL_NUM = Finacle_Lrs.SHDL_NUM
and a.FLOW_ID in ('PRDEM', 'EIDEM') AND a.ACTIVE_FLAG = 'Y' AND a.DELETE_FLAG='N' and a.bank_id = @bank_id
);
end
return isnull(@lMaturityDate,null);
END
做完一些检查后,我注意到在比较oracle数据库上的原始代码时,有一天低估了19629行。请查看oracle版本的代码:
FUNCTION GetLoanMaturityDate(AcctAcid VARCHAR2, ScheduleNum VARCHAR2) RETURN DATE IS
lMaturityDate DATE;
BEGIN
begin
select decode(lrs.LR_FREQ_TYPE, 'W', lrs.FLOW_START_DATE + (lrs.NUM_OF_FLOWS - 1) * 7,
'F', lrs.FLOW_START_DATE + (lrs.NUM_OF_FLOWS - 1) * 14,
'M', add_months(lrs.FLOW_START_DATE, lrs.NUM_OF_FLOWS - 1) ,
'Q', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 3),
'H', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 6),
'Y', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 12)
)
into lMaturityDate
from lrs
where lrs.acid = AcctAcid
and lrs.shdl_num = ScheduleNum
and lrs.FLOW_ID in ('PRDEM','EIDEM')
and lrs.FLOW_START_DATE = ( select max(a.FLOW_START_DATE)
from lrs a
where a.ACID = lrs.ACID
and a.SHDL_NUM = lrs.SHDL_NUM
and a.FLOW_ID in ('PRDEM', 'EIDEM')
) ;
exception
when no_data_found then
lMaturityDate:=NULL;
end;
return lMaturityDate;
END GetLoanMaturityDate;
这也是我运行以查看不正确行的比较查询:
SELECT eim.acid as eim_acid,it.[ACID] as it_acid,
eim.[MATURITY_DATE] as eim_maturity_date,it.[MATURITY_DATE] as it_maturity_date ,
eim.[DAYS_TO_MATURITY] as eim_DAYS_TO_MATURITY,it.[DAYS_TO_MATURITY] as it_DAYS_TO_MATURITY,(it.[DAYS_TO_MATURITY]-eim.[DAYS_TO_MATURITY]) as diff
FROM [TEST_EIMDW_BOJ_REPORTS].[BOJOWN].[loans_maturity_profile] eim
inner join [dbo].[IT_LOANS_MATURITY_PROFILE] it on eim.acid= it.acid
where eim.MATURITY_DATE != it.MATURITY_DATE
最后,下面是查询结果,显示了正确结果集的比较与我当前试图在sql服务器上修复的结果的比较:
答案 0 :(得分:2)
Oracle的add_months()
和SQL Server的dateaddd(month)
具有不同的语义,尤其是在月底。
所以:
select add_months(date '2019-02-28', 1)
from dual
--> 2019-03-31
(请参阅db<>fiddle)
位置:
select dateadd(month, 1, '2019-02-28')
--> 2019-03-28
(请参阅db<>fiddle)
在SQL Server中模拟Oracle行为可能会有些棘手。我什至不确定这是否可取。
答案 1 :(得分:2)
就像戈登所说的那样,Oracle ADD_MONTHS
函数的确切行为是,如果源日期是该月的最后一天,则返回日期也是结果月份的最后一天(不论来源日期的月长短。
SQL Server没有类似的功能。
哪种实现是正确的,或者是否重要,将取决于您的特定情况。
答案 2 :(得分:0)
SQL Server“月末”-EOMONTH(start_date [,month_to_add])函数在该月的最后一天的行为类似于ORACLE add_months。