Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Locating MAX of multiple date columns

$
0
0

I have a table with five date columns, I'm using a COALESCE CASE WHEN (example below) to produce an alias with the value of the most recent of the five date columns.  That part works fine but what I'm having difficulty with is applying this in my WHERE statement because I cant use the alias in my WHERE statement.  I need to be able to use the newest of the five columns and see if it's older than 5 years.  There has to be a simple way that my google searches is not finding :)

What I am using now to find the newest of the date values:

COALESCE(CASE WHEN hi.dose5date IS NULL THEN hi.dose4date ELSE hi.dose5date end,
CASE WHEN hi.dose5date IS NULL AND hi.dose4date IS NULL THEN hi.dose3date end,
CASE WHEN hi.dose5date IS NULL AND hi.dose4date IS NULL AND hi.dose3date IS NULL THEN hi.dose2date end,
CASE WHEN hi.dose5date IS NULL AND hi.dose4date IS NULL AND hi.dose3date IS NULL AND hi.dose2date IS NULL THEN hi.dose1date end) AS MostRecentVacc

Your help is most appreciated :)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>