|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Sunday, April 26, 2009 7:37 AM
Points: 87,
Visits: 307
|
|
I have table tbSales where the is datatype numeric(8,0). When I run this script I keep getting this error :-
Msg 8115, Level 16, State 2, Line 4 Arithmetic overflow error converting expression to data type datetime.
DECLARE @FirstDay DATETIME,@LastDay DATETIME SET @FirstDay =(SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())-0,0 )) SET @LastDay =(SELECT DATEADD(MONTH,DATEDIFF(MONTH,-1,GETDATE())-0,-1)) SELECT Loanreference ,BranchCode ,Firstdisbursementdate ,LoanOfficerCode ,[#ofLoans] ,LoanId ,IDNumber ,ClientNumber ,StartDate ,CreationDate ,CreatedBy FROM dbo.tbSales WHERE CONVERT(DATETIME,Firstdisbursementdate,120) BETWEEN @FirstDay AND @LastDay AND ISDATE(Firstdisbursementdate) = 1
I have tried to filter out the invalid dates which are just Zeros but I keep getting this error. How do I solve this
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, April 23, 2012 7:35 AM
Points: 373,
Visits: 761
|
|
| I would look at the isdate() in the where clause. Is it possible that the query evaluates the first condition and fails, before it eliminates the isdate() criteria. Maybe filter by isdate() into a temp table or result set and then apply the next criteria to that.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 11:17 AM
Points: 9,855,
Visits: 9,376
|
|
Try it like this:
DECLARE @FirstDay DATETIME,@LastDay DATETIME SET @FirstDay =(SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())-0,0 )) SET @LastDay =(SELECT DATEADD(MONTH,DATEDIFF(MONTH,-1,GETDATE())-0,-1)) SELECT Loanreference ,BranchCode ,Firstdisbursementdate ,LoanOfficerCode ,[#ofLoans] ,LoanId ,IDNumber ,ClientNumber ,StartDate ,CreationDate ,CreatedBy FROM dbo.tbSales WHERE Case When ISDATE(Firstdisbursementdate) = 1 Then CONVERT(DATETIME,Firstdisbursementdate,120) Else Cast(0 as datetime) End BETWEEN @FirstDay AND @LastDay
-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung Proactive Performance Solutions, Inc. "Performance is our middle name."
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, September 12, 2012 5:17 AM
Points: 329,
Visits: 461
|
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Sunday, April 26, 2009 7:37 AM
Points: 87,
Visits: 307
|
|
I tried using
FROM dbo.tbSales WHERE Case When ISDATE(Firstdisbursementdate) = 1 AND LEN(Firstdisbursementdate)>= 8
Then CONVERT(DATETIME,Firstdisbursementdate,120) Else Cast(0 as datetime) End BETWEEN @FirstDay AND @LastDay and I still get the same error..
Msg 8115, Level 16, State 2, Line 4 Arithmetic overflow error converting expression to data type datetime.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 11:17 AM
Points: 9,855,
Visits: 9,376
|
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 11:17 AM
Points: 9,855,
Visits: 9,376
|
|
I did read the article at your blog, but there were no examples of ISDATE() being unreliable. If you could supply some examples of ISDATE() returning results inconsistent with what CONVERT(DateTime, {string}, style) can convert successfully (asuming style is not 0,9,100, or 109), that would be helpful. However, for now, ISDATE appears to be perfectly reliable to me.
-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung Proactive Performance Solutions, Inc. "Performance is our middle name."
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Sunday, April 26, 2009 7:37 AM
Points: 87,
Visits: 307
|
|
Hi I did a check the non date valuse are zeros ......used isdate to determine non valid dates
--select distinct Firstdisbursementdate --from tbSales --where isdate(Firstdisbursementdate) =1
--select distinct Firstdisbursementdate --from tbSales --where isdate(Firstdisbursementdate) !=1
Valid dates Firstdisbursementdate
20020405 20041222 20001111 20060223 19991110 19980425 19991118 20080723 20020813
Non Valid dates Firstdisbursementdate 0 0 0 0 0 0 0
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 11:17 AM
Points: 9,855,
Visits: 9,376
|
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Sunday, April 26, 2009 7:37 AM
Points: 87,
Visits: 307
|
|
|
|
|