0

I am trying to get days difference between 2 days and I am getting below error. Could any one please help

CONVERT(DECIMAL,((dbo.convertToUnixTime(GETDATE())-p.CREATEDDATE)))

Msg 8115, Level 16, State 2, Line 79

Arithmetic overflow error converting expression to data type datetime.

6
  • Which dbms are you using? (Those functions are product specific.) Commented Sep 25, 2020 at 9:14
  • Hi @jarlh I am using sql server Commented Sep 25, 2020 at 9:15
  • CREATEDDATE data type? Commented Sep 25, 2020 at 9:17
  • @jarlh its datetime Commented Sep 25, 2020 at 9:19
  • What if the definition of dbo.convertToUnixTime? Why are you defining your value as just decimal, where the precision and scale? Why do you appear to be subtracting a date away from another? Dates aren't numbers, it doesn't make sense to "subtract" 2020-09-17T19:12:59.462 from 2020-09-25T10:47:04.123. Commented Sep 25, 2020 at 9:47

3 Answers 3

0

If you want fractional days, then you can take the difference in another unit, say seconds, and divide:

SELECT DATEDIFF(second, p.CREATEDATE, GETDATE()) / (24.0 * 60 * 60)

Note that for DATEDIFF() the older date goes first. I assume p.CREATEDATE is in the past and you want a positive number.

If you are dealing with dates far in the past or the future, you might want to use minute rather than second or alternatively DATEDIFF_BIG().

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

Comments

0

Use the date functions:

SELECT DATEDIFF(day,GETDATE(),p.CREATEDATE)

3 Comments

Hi @wouter .I did this but I need to see my days in decimals too.But I am not getting that.
Then do a DATEDIFF in hours and divide by 24 , or difference in minutes, seconds, etc. depending on how precise you want it.
Btw if you want to use your convertToUnixTime function, then you should convert both datetimes (the getdate() and the CREATEDATE). But that will be much less efficient.
0

datediff() always return an integer value, expressed in the unit your are giving as first argument.

If you want something accurate, you can compute the date difference in seconds, then use artithmetics to convert to a number of days:

datediff(second, p.createdate, getdate()) / 60.0 / 60 / 24

Comments

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.