【问题标题】:SSRS/SQL: Conversion failed when converting date and/or time from character stringSSRS/SQL:从字符串转换日期和/或时间时转换失败
【发布时间】:2015-03-30 07:10:34
【问题描述】:

我的 ssrs 报告中有两个日期(日历)参数。我有日期以 yyyyMMdd 格式存储在我的数据库中。我尝试了许多组合,例如将存储过程中的参数类型从 varchar 更改为 datetime ,反之亦然,但似乎都没有。

create procedure Proc_GetInterestLetterTotal_NEWSSRS         
      @LasAcctNo varchar(20)='',                                          
      @From datetime,                                          
      @To datetime                    
    as       

    DECLARE @Fromdt varchar(8)  
    DECLARE @Todt varchar(8)  

    select @Fromdt=RIGHT(@From,4) + '' 
                   + SUBSTRING(CONVERT(varchar,@From),4,2) 
                   + '' 
                   + LEFT(@From,2);  
    select @Todt=RIGHT(@To,4) 
                   + ''
                   +  SUBSTRING(CONVERT(varchar,@To),4,2) 
                   + '' 
                   + LEFT(@To,2);  

    with cte (InterestCharged,InterestRepaid,InterestAccruedbutnotdue
              ,OutstandingInterest,InterestAdjusted) AS                 
    (                     
    select sum(amt) as InterestCharged
           ,'0' InterestRepaid
           ,'0' InterestAccruedbutnotdue
           ,'0' OutstandingInterest
           ,'0' InterestAdjusted 
    from nbfcledger 
    where  accountid=@LasAcctNo 
    and valuedate between  @Fromdt and @Todt                
    and dr_cr='D' and GlCode='INTRAC' 
    and CMtxnType='INTPOST'   --interestcharged            
    union                
    select '0' InterestCharged
           , sum(amt) as InterestRepaid
           ,'0' InterestAccruedbutnotdue
           ,'0' OutstandingInterest
           ,'0' InterestAdjusted  
    from nbfcledger 
    where  accountid=@LasAcctNo
    and valuedate between  @Fromdt and @Todt                
    and dr_cr='C' and GlCode='INTRAC' 
    and CMtxnType in ('RAI','RECEIPT OF')  --interest Paid          
    union                
    ----select '0' InterestCharged
        --       ,'0' InterestRepaid
        --       , sum(InterestAccruedbutnotdue) as InterestAccruedbutnotdue
        --       ,'0' OutstandingInterest
        --       ,'0' InterestAdjusted 
     --from  Tbl_InterestWorkingDaily               
    ----where  accountid=@LasAcctNo    
    --and( convert(datetime,dt) between  @From  and  @to) --        
    select '0' InterestCharged
           ,'0' InterestRepaid
           , sum(Amt) as InterestAccruedbutnotdue
           ,'0' OutstandingInterest
           ,'0' InterestAdjusted           
    from nbfcledger 
    where accountid=@LasAcctNo 
    and GLCode='INTRAC' 
    and valuedate between @Fromdt and @Todt 
    and dr_cr='D'               
    union                
    select '0' InterestCharged
           ,'0' InterestRepaid
           , '0' as InterestAccruedbutnotdue
           ,CONVERT(numeric(18,2),ROUND( sum( case when Dr_Cr='D' then Amt else Amt *-1  end  ),2)) 
            as OutStandingInterest
           ,0 InterestAdjusted 
    from nbfcledger             
    where accountid=@LasAcctNo
    and glcode='INTRAC'   
    and valuedate <=@todt    --Outstanding Interest            
    union                    
    select '0' InterestCharged
          , '0' as InterestRepaid
          ,'0' InterestAccruedbutnotdue
          ,'0' OutstandingInterest
          ,sum(amt) InterestAdjusted  
    from nbfcledger 
    where  accountid=@LasAcctNo 
    and valuedate between  @Fromdt and @Todt                
    and dr_cr='C'
    and GlCode in ('INTRAC','PNLINT') 
    and CMtxnType in ('JV')  --Interest Adjusted          
    )                

    select sum(InterestCharged) as TotalInterestCharged
         , sum(InterestCharged)-sum(InterestAdjusted)
           as InterestCharged
         , Sum(InterestRepaid) 
           as InterestRepaid
         ,sum(InterestAccruedbutnotdue) 
           as InterestAccruedbutnotdue      
         ,sum(OutstandingInterest) 
           as OutstandingInterest
         ,sum(InterestAdjusted) 
           as InterestAdjusted
        , CONVERT(VARCHAR(10),convert(datetime,@Fromdt),103) as FromDt
        , CONVERT(VARCHAR(10),convert(datetime,@Todt),103) as ToDate  
     from cte     

【问题讨论】:

  • 你用的是什么版本?您数据库中的日期也是VARCHAR 类型?
  • @wewestthemenace Report builder 3.0 and Sql server 2005 yes date in database are varchar(8)
  • 您是不是因为nbfcledger 表中的valuedate 是varchar(8) yyyyMMdd 格式而将参数@From datetime 转换为varchar(8)?
  • 尝试使用 convert(DATETIME, valuedate, 112) 转换您的 valuedate。
  • 您有 2 个 datetime 参数,您将它们转换为 varchars 但没有使用内置格式,然后您尝试将它们转换回日期并将它们转换为另一种 varchar 格式,为什么?

标签: sql sql-server reporting-services


【解决方案1】:

尝试将其用于局部变量

select @Fromdt= cast(year(@from) as varchar(4)) + cast(month(@from) as varchar(2)) + cast(day(@from) as varchar(2))

如果你使用 Right("datetime",4) 你可能会得到毫秒 + 'AM/PM' 例如:

declare @a datetime
select @a = '2015-03-30'
select right(@a,4)  >> output 00AM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多