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

时间:2015-03-30 07:10:34

标签: sql sql-server reporting-services

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

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     

1 个答案:

答案 0 :(得分:0)

尝试将此用于局部变量

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

如果你使用Right(&#34; datetime&#34;,4)你可能会得到几毫秒+上午/下午&#39; 例如:

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