Difference between + en - in date statement

Posted by Lien on 27-Feb-2017 07:44

Hey,

Why is there a difference in usesage of a + or a - with a date context?

For example:

define variable l-date as date no-undo.

l-date = today + 1.

=> 28/02/17 = >ok

l-date = 1 + today.

=> 28/02/17 => ok

l-date = today - 1

=> 26/02/17 =>ok 

l-date = 1 - today

=> nok

---------------------------

Error (Press HELP to view stack trace)
---------------------------
** Incompatible data types in expression or assignment. (223)
** Could not understand line 3. (196)
---------------------------
OK Help
---------------------------

All Replies

Posted by martinz on 27-Feb-2017 07:51

Lien,

What would be the desired result of the expression that fails?

Posted by Lien on 27-Feb-2017 07:54

The same result als today - 1.

Which normally dosn't matter in progress which order the expression is displayed except when you use brackets

- See more at: community.progress.com/.../100289

Posted by gdb390 on 27-Feb-2017 07:56

You can't expect a date to become negative

Rather then using +1 and -1 , use the add-interval

Posted by Brian K. Maher on 27-Feb-2017 08:00

Lien,
 
That would be wrong.  “today – 1” is very much different than “1 – today”.
 
Since dates in the ABL are integer values from a base date, let us assume today = serial date 12345.  Under that assumption, you are stating that “12345 – 1” (12344) should be the same as 1 – 12345) (-12344).
 
Brian
 

Posted by Simon L. Prinsloo on 27-Feb-2017 08:07

The order of the operands does not matter for + and *, but it does for - and /.

10 + 1 = 11

1 + 10 = 11

10 - 1 = 9

1 - 10 = -9

 

l-date = today - 1   is equivalent to  l-date = DATE(2457813 - 1) 

l-date = 1 - today   is equivalent to  l-date = DATE(1 - 2457813) 

So the first one returns DATE(2457812), which is 26 February 2017 AD.

The second one returns DATE( -2457812 ), which is 16 November 11443 BC.

Posted by Frank Meulblok on 27-Feb-2017 08:42

The fact that the "1 - today" doesn't work probably should be reported as a Tech Support case.

There's 2 implicit data type conversions here: from date to integer to resolve the expression, and then back from integer to date for the variable assignment.  

If the other variants support the implicit data type conversions to make this work, there's no reason this one shouldn't so this can be considered a defect.

I'll second  gdb390's suggestion to use add-interval for any time-based calculations though.

That keeps the intent clearer in my eyes.

Posted by Matt Gilarde on 27-Feb-2017 09:17

Wouldn't 1 - TODAY result in an integer value (the number of days between DATE(1) and today)? If so, the error is correct because you're trying to put an integer result into a date variable.

Posted by martinz on 27-Feb-2017 09:19

Though 1 - today could (should?) be valid, I can't imagine myself intentionally writing such an expression. If I wrote 1 - today, I'd be glad the compiler told me about my probable mistake.

How about TODAY - TODAY vs. TODAY + TODAY? Which should compile, which shouldn't and why?

This thread is closed