July 21, 2004 at 5:21 pm
I had a problem getting between to work correctly. Example:
case
when datediff(ss, x, y) between -3 and 3 then 'Y'
else 'N'
end
does not work as expected.
I changed to:
case
when datediff(ss, x, y) >= -3 and datediff(ss, x, y) <= 3 then 'Y'
else 'N'
end
This works fine. Is there some problem with between not working correctly with negative values? or am I missing something very basic?
July 21, 2004 at 6:08 pm
July 21, 2004 at 6:38 pm
Yeah, I've seen this too for the case posted ...
It seems as if though SQL Server is expecting the start_value and end_value of BETWEEN to be the same sign ...
So, for the BETWEEN to work for the case posted, it would need to be broken into two BETWEENs:
CASE
WHEN DATEDIFF(ss, x, y) BETWEEN -3 AND -1 OR DATEDIFF(ss, x, y) BETWEEN 0 AND 3 THEN 'Y' ELSE 'N' END
I always try to use the >= and <= operators for ranges whenever possible.
JP
July 21, 2004 at 8:49 pm
WHEN ABS(DATEDIFF(ss, x, y) ) <= 3
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply