1

I have a calculated column which is updated when two other columns are updated and the formula for this column is

SELECT(datediff(day,'2012-11-01 18:15:00.000','2012-10-22 11:59:58.000'))

It works fine for all other dates but when the month changes, then I get a negative value. How can I make sure this does not happen and I get the correct integer value?

1 Answer 1

1

Well, two options:

  1. just make sure the earlier date is always the first in your list of dates for DATEDIFF

    SELECT DATEDIFF(DAY, '2012-11-01 18:15:00.000', '2012-10-22 11:59:58.000')
    

    This gives you a result of -10 - this is because the first date in your list is the later date.

    If you use

    SELECT DATEDIFF(DAY, '2012-10-22 11:59:58.000', '2012-11-01 18:15:00.000')
    

    you get the result of 10 - since the first date in DATEDIFF is the earlier date.

  2. just use ABS to get the absolute value (without the + or -):

    SELECT ABS(DATEDIFF(DAY, '2012-11-01 18:15:00.000', '2012-10-22 11:59:58.000'))
    

    In that case, you never get a negative value.

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

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.