0

I am trying to run this simple query to get sum of all amounts for last year and getting error. Can someone shed some light on this please. I am not doing any casting and TX_DATE column is of type date.

SELECT SUM(AMOUNT) as TOTAL
FROM
    TRANSACTIONS
WHERE
    ID = '12345'
    AND TYPE = 'Amount'
    AND DATEDIFF(MONTH,TX_DATE,GETDATE()) <= 12

Error:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Arithmetic overflow error converting expression to data type datetime.
    at   com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:4853)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1781)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:1034)
    at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
    at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:91)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:446)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:396)
    ... 50 more
4
  • 1
    Could TX_DATE be a non DATE or DATETIME field type perhaps? Commented Jun 26, 2014 at 11:59
  • Yes. Indeed. Its nvarchar in UAT env and Date in my own evn ;) Commented Jun 26, 2014 at 12:02
  • Seems it contains garbage dates then... :) Commented Jun 26, 2014 at 12:03
  • Well as you are using Datediff for datepart month chances of return value which is out of range for int is doubtful. IS this the only query which is getting fired or is it part of some big query. Also find out Max and Min dates of TX_DATE from TRANSACTIONS table. May be that helps. Commented Jun 26, 2014 at 12:31

1 Answer 1

1

Check type of TX_DATE. It seems its not a date.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, TX_DATE is not of type date (checked after I posted the question). The thing that am still not clear about is why the query does not fail when run via Microsoft SQL Server Management Studio but fails when run via jdbc.
These are two different systems so it all depends on how the value of TX_DATE was interpreted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.