执行视图时出错(从字符串转换日期和/或时间时转换失败)

时间:2018-11-12 09:40:35

标签: sql sql-server-2016

执行此视图时出现此错误:-

  

从字符串转换日期和/或时间时转换失败

请帮助我解决在此给定查询中出现的错误类型?*

SQL查询:

WITH CTE_aacd1 AS (
    SELECT *
    FROM (
        SELECT LEAD_CO_MNE
                ,ACCOUNT_NO
                ,CLOSURE_TYPE
                ,DATE_TIME
                ,Rank() OVER (ORDER BY [DATE_TIME]) AS rank_
        FROM [InsightSource].BS.AA_ACCOUNT_CLOSURE_DETAILS
        ) aat
    WHERE aat.rank_ = 1
    )

SELECT      [Date].BusinessDate as [BusinessDate]               , aa.LEAD_CO_MNE as LeadCompany     , 'BS' as SourceSystem      , concat('BS:' , com.FINANCIAL_MNE , ':' , cast(aa.[LINKED_APPL_ID] as nvarchar(50))) as [SourceAccountId]  , concat('BS:' ,CO_CHK.COMPANY_MNE   , ':' , CAST(isnull(A.[CO_CODE], aa.[CO_CODE]) as nvarchar(50))) as [SourceBranchId]   , concat('BS:' , COM.CUSTOMER_MNEMONIC , ':' , cast(isnull(A.[CUSTOMER], aa.[CUSTOMER]) as nvarchar(50))) as [SourceCustomerId]     , concat('BS:' , CO_CHK.COMPANY_MNE  , ':' , cast(A.[ACCOUNT_OFFICER] as nvarchar(50))) as [SourceEmployeeId]   , concat('BS:' , com.CUSTOMER_MNEMONIC , ':' , l.[@id]) as [SourceLimitId]  --,A.[CATEGORY] as [sourceGLId]             ,aa.[LINKED_APPL_ID] as [AccountNum]             
    ,-1 * Rb.Debitbal as Balance    ,-1 * rb.ForeignDebitBal as [ForeignCurrencyBal]    ,'Loan' as [Category]   ,A.[CURRENCY] as [Currency]                 ,-1 * rb.InterestAccrued as [InterestAccrued]           ,rb.InterestRate as InterestRate    ,rb.FixOrVAr    ,rb.Rate_tier_type as RateType      --,rb.InterestRateIndex     ,rb.InterestRateVariance    ,p.[@ID] as [ProductCode]               ,P.DESCRIPTION as [ProductDesc]                 ,aa.PRODUCT_GROUP as [ProductType]  ,aa.PRODUCT_GROUP as [T24ProductGroup]  ,aa.[START_DATE] as [StartDate]     ,isnull(aa.ORIG_CONTRACT_DATE, aa.[START_DATE]) OriginalStartDate   ,case when a.INACTIV_MARKER  = 'Y' then 'InActive' else AA.[ARR_STATUS] end as [StatusCode]                 ,a.[ACCOUNT_TITLE_1] as [StatementDesc]     ,isnull(aacd.RENEWAL_DATE, aacd.MATURITY_DATE) as MaturityDate  ,cast(aacd.MATURITY_DATE as date) as AmortMatureDate    ,cast(aata.[AMOUNT] as decimal (28,4)) as OriginalLoanAmount    ,cast(aata.[AMOUNT] as decimal (28,4)) as Authorized    ,right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) as TermUnit     ,try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 )) as Term   ,case when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'M' then         try_convert(int,SUBSTRING(Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))         when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'D' then          try_convert(int,round(try_convert(int,SUBSTRING(Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*(1/(365.25/12)) , 0))         when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'Y' then          try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*12 , 0))
    else NULL end as TermInMonths,  case when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'M' then          try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*30.44,0))        when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'D' then          try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))        when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'Y' then          try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*365.25 , 0))     else NULL end as TermInDays    ,aacd.[START_DATE] as DisburseDate      ,case when aacd.ARR_AGE_STATUS = 'DEL' then 'Yes' else 'No' end as IsDelinquent     ,del.DelinquentAmount   ,del.LastDelinquentDate     -- (!!! ERROR !!!),case when LastDelinquentDate is not null and isdate(del.LastDelinquentDate) = 1 then datediff( d, del.LastDelinquentDate, date.BusinessDate ) end as DelinquentDays  ,case       when (LastDelinquentDate is not null) and (TRY_CONVERT(DATE, del.LastDelinquentDate) IS NOT NULL)           then datediff(d, TRY_CONVERT(DATE, del.LastDelinquentDate), [date].BusinessDate )    end as DelinquentDays  ,aacd1.CLOSURE_TYPE as ReasonClosed     ,cast(left('20' + aacd1.DATE_TIME, 8) as date) as ClosedDate    ,-1 * rb.DebitBal as [AvailableFunds]   ,left(arrschedule.Start_date, 8) as FirstPmtDate    ,arrschedule.Calc_Amount as ScheduledPmtAmt         ,case when payment_freq like '%e1M%' and payment_freq not like '%o%,%D%' then 'Monthly'         when payment_freq like '%e1M%' and payment_freq like '%o%,%D%' then 'Twice a month'         when payment_freq like '%e2M%' and payment_freq not like '%o%,%D%' then 'Every 2 month'         when payment_freq like '%e2M%' and payment_freq like '%o%,%D%' then 'Twice every 2 month'       when payment_freq like '%e1W%' or payment_freq like  '%e7D%' then 'Weekly'          when payment_freq like '%e2W%' or payment_freq like  '%e14D%'  then 'Bi-Weekly'     else 'N/A' end as PmtFreq   ,case when intfreq like '%e1M%' and intfreq not like '%o%,%D%' then 'Monthly'       when intFreq like '%e1M%' and intfreq like '%o%,%D%' then 'Twice a month'       when intFreq like '%e2M%' and intfreq not like '%o%,%D%' then 'Every 2 month'       when intFreq like '%e2M%' and intfreq like '%o%,%D%' then 'Twice every 2 month'         when payment_freq like '%e1W%' or payment_freq like  '%e7D%' then 'Weekly'          when payment_freq like '%e2W%' or payment_freq like  '%e14D%'  then 'Bi-Weekly'     else 'N/A' end as InterestPaidFreq      , arrschedule.payment_type as PmtCalcMethod     ,'BSAA_Lending' as SystemSource  FROM [InsightSource].[BS].[AA_ARRANGEMENT] aa   LEFT JOIN [InsightSource].[BS].[COMPANY] COM   ON COM.MNEMONIC = AA.BRANCH_CO_MNE   LEFT JOIN [InsightSource].[BS].[COMPANY_CHECK_Company_Mne] CO_CHK      ON CO_CHK.[@ID] = 'MASTER' and CO_CHK.[Sequence] = 1    LEFT JOIN [InsightSource].[BS].[ACCOUNT] a      ON a.LEAD_CO_MNE = COM.FINANCIAL_MNE AND A.[@ID] = aa.LINKED_APPL_ID   LEFT JOIN [InsightSource].BS.LIMIT l     ON a.CUSTOMER +'.' + RIGHT ( '0000' + a.LIMIT_REF , 10) = l.[@ID] AND l.LEAD_CO_MNE = COM.CUSTOMER_MNEMONIC  LEFT JOIN  (Select del1.[@id], DEL1.ARRANGEMENT_ID, Del1.LEAD_CO_MNE, del1.LastDelinquentDate, SUM(cast(bd.OS_TOTAL_AMOUNT as decimal (28,4))) as DelinquentAmount from 
            (select aad.[@id], AAD.LEAD_CO_MNE, AAD.ARRANGEMENT_ID, min(billpay.Bill_Pay_date) as LastDelinquentDate, max(billpay.Bill_Pay_date) as MaxDelinquentDate
      from
            ( select [@id], [LEAD_CO_MNE], ARR_AGE_STATUS, [@ID] AS ARRANGEMENT_ID, BILL_PAY_DATE from [InsightSource].BS.AA_ACCOUNT_DETAILS where ARR_AGE_STATUS in ('NAB', 'DEL')) aad join
            ( select * from [InsightSource].BS.AA_ACCOUNT_DETAILS_Bill_Pay_date where Aging_Status
= 'DEL' or bill_status = 'Aging') billpay on aad.[@id] = billpay.[@id] and aad.[LEAD_CO_MNE] = billpay.[LEAD_CO_MNE]  
                  group by aad.[@id], AAD.ARRANGEMENT_ID, aad.[LEAD_CO_MNE]
            ) Del1 
      join
            [InsightSource].BS.AA_BILL_DETAILS bd on del1.LEAD_CO_MNE = bd.LEAD_CO_MNE AND  bd.[ARRANGEMENT_ID] = DEL1.ARRANGEMENT_ID --and del1.MaxDelinquentDate = bd.actual_pay_date             Group by del1.[@id], DEL1.ARRANGEMENT_ID, del1.[LEAD_CO_MNE], del1.LastDelinquentDate   )del    ON AA.LEAD_CO_MNE = DEL.LEAD_CO_MNE AND AA.[@ID] = DEL.ARRANGEMENT_ID LEFT JOIN     (select  LEAD_CO_MNE, ID_COMP_1, replace (max(CHANGE_PERIOD) , 'R_BIRTH + ' , '') as  CHANGE_PERIOD from [InsightSource].BS.AA_ARR_CHANGE_PRODUCT arr1  where ID_COMP_3 = (select MAX (ID_COMP_3) from [InsightSource].BS.AA_ARR_CHANGE_PRODUCT arr2 where arr1.ID_COMP_1 = arr2.ID_COMP_1 )    group by ID_COMP_1, LEAD_CO_MNE     ) aacp      ON COM.FINANCIAL_MNE = aacp.LEAD_CO_MNE AND AA.[@ID] = aacp.ID_Comp_1   LEFT JOIN CTE_aacd1 aacd1   ON aacd1.LEAD_CO_MNE = COM.CUSTOMER_MNEMONIC AND aacd1.ACCOUNT_NO = A.[@ID]   LEFT JOIN (           SELECT *            FROM (          SELECT [@ID]
                            ,[LEAD_CO_MNE]
                            ,PRODUCT
                            ,PRODUCT_STATUS
                            ,Rank() OVER (partition by [@id]
                                ORDER BY [PROD_EFF_DATE]
                                ) AS rank_
                    FROM [InsightSource].BS.AA_ARRANGEMENT_PRODUCT
                    where [PRODUCT_STATUS] = 'CURRENT'
                    ) aap1          WHERE aap1.rank_ = 1            ) aap           ON aa.[@ID] = aap.[@ID]             AND AAP.[LEAD_CO_MNE] = COM.[FINANCIAL_MNE]

    LEFT JOIN  [InsightSource].BS.AA_PRODUCT p      ON CASE WHEN COM.SPCL_FIN_FILE IS NULL THEN COM.DEFAULT_FINAN_MNE ELSE COM.SPCL_FIN_MNE END = P.BRANCH_CO_MNE AND AAP.PRODUCT = P.[@ID]     LEFT JOIN [InsightSource].BS.AA_ACCOUNT_DETAILS aacd    ON aacd.[LEAD_CO_MNE] = COM.FINANCIAL_MNE AND aacd.[@ID] = aa.[@ID]     LEFT JOIN       (   select              ps.[@id] ,ps.LEAD_CO_MNE, ps.ID_Comp_1 , ps.Base_date, sfi.Calc_Amount, sf.Actual_amt, sf.payment_freq , sf.payment_type , ps.Start_date, SFI.DUE_FREQ as intFreq           ,case when sf.payment_freq  like '%e1M%' and sf.payment_freq  not like '%o%,%D%' then 1
                  when sf.payment_freq  like '%e1M%' and sf.payment_freq  like '%o%,%D%' then 2
                  when sf.payment_freq  like '%e2M%' and sf.payment_freq  like '%o%,%D%' then 1
                  when sf.payment_freq  like '%e1W%' then 4
                  when sf.payment_freq  like '%e2W%' then 2
             end as NumPmtInOneMonth        from            [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE ps join              [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE_payment_freq sf on 
                ps.[@id] = sf.[@id] and ps.[LEAD_CO_MNE] = sf.[LEAD_CO_MNE] and sequence = 1 left join              [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE_payment_freq_property sfi            on ps.[@id] = sfi.[@id] and ps.[lead_co_mne] = sfi.[lead_co_mne] and sfi.property = 'INTEREST' and sfi.MVsequence = 1 and sfi.sequence = 1          where           ps.id_comp_3 = (select MAX (id_comp_3) from [InsightSource].BS.AA_ARR_PAYMENT_SCHEDULE arr2 where ps.ID_COMP_1 = arr2.ID_COMP_1 )       and sfi.Calc_Amount is not null         group by ps.LEAD_CO_MNE,ps.[@id] , ps.ID_Comp_1, ps.Base_date, sfi.Calc_Amount, sf.Actual_amt, sf.payment_freq , sf.payment_type , ps.Start_date ,SFI.DUE_FREQ, [bill_produced]     ) arrSchedule   ON aa.LEAD_CO_MNE = arrSchedule.LEAD_CO_MNE AND AA.[@ID] = arrSchedule.ID_Comp_1    LEFT JOIN   (select  LEAD_CO_MNE,max(REVOLVING) as REVOLVING, max(TERM) as TERM, max(AMOUNT) as amount, ID_COMP_1 from [InsightSource].BS.AA_ARR_TERM_AMOUNT arr1       where ID_COMP_3 = (select MAX (ID_COMP_3) from [InsightSource].BS.AA_ARR_TERM_AMOUNT arr2 where arr1.ID_COMP_1 = arr2.ID_COMP_1 )       group by ID_COMP_1,LEAD_CO_MNE  ) aata      ON COM.FINANCIAL_MNE = aata.LEAD_CO_MNE AND AA.[@ID] = aata.ID_Comp_1   LEFT JOIN [v_sourceAccountBSAA_RatesandBalances] rb     ON rb.[Linked_appl_id] = aa.LINKED_APPL_ID  and rb.Lead_co_mne = aa.LEAD_CO_MNE     CROSS JOIN [InsightStaging].[dbo].[v_sourceDate] as [Date]     WHERE aa.PRODUCT_LINE = 'LENDING' and aa.[LINKED_APPL_ID] is not null

0 个答案:

没有答案