case when cast(rh.RECV_DATE-pl.DUE_DATE as integer) > 5 or cast(rh.RECV_DATE-pl.DUE_DATE as integer) < -5 then 'Out of Limits'
else 'Within Limits'
end
Are those columns datetime data type? If so, you should be using DATEADD function on them. Which version of SQL Server are you using?
You can use outer apply to tidy up a bit and you can further reference in the select statement if needed.
SELECT rtrim(PARTNUMBER) as [Part Number]
, a.DaysLate as [Days Late]
, case when a.DaysLate > 5 or a.DaysLate < -5 then 'Out of Limits' else 'Within Limits' end as [Check]
from YourTable
outer apply
(
select cast(RECV_DATE-DUE_DATE as integer)
) a(DaysLate);
I believe it only breaks if you change the data type from datetime to datetime2 - but it would be much better to use date functions anyways to prevent code breaking in a future version when the data type is changed.