The idea here is to use the implication rewrite rule:
IF ( x ) THEN ( y ) is equivalent to ( NOT ( x ) OR y )
In your case
IF ( DATEPART(HOUR, CURRENT_TIMESTAMP) >= 16 ) THEN ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 1 )
equivalently
( NOT ( DATEPART(HOUR, CURRENT_TIMESTAMP) >= 16 ) OR ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 1 ) )
and itself is equivalent
( ( DATEPART(HOUR, CURRENT_TIMESTAMP) < 16 ) OR ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 1 ) )
Rewriting the original ELSE as an independent IF..THEN expression:
IF ( DATEPART(HOUR, CURRENT_TIMESTAMP) < 16 ) THEN ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 0 )
equivalent (this time skipping the intermediate step)
( ( DATEPART(HOUR, CURRENT_TIMESTAMP) >= 16 ) OR ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 0 ) )
Then two expressions can be written in conjunctive normal form ("a series of AND s)
SELECT * FROM the_table WHERE ( ( DATEPART(HOUR, CURRENT_TIMESTAMP) < 16 ) OR ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 1 ) ) AND ( ( (DATEPART(HOUR, CURRENT_TIMESTAMP) >= 16 OR ( DATEDIFF(DAY, CURRENT_TIMESTAMP, MailDate) = 0 ) ) ;
onedaywhen
source share