I got a table with multiple columns that contain DateTime namely
gendate1 up to
gendate8. In my stored procedure, the user will have to input a date (
@date_start). Now I have to compare the inputted date to the 8 dates which will filter all the data. The problem is that I have to get the two closest dates (out of eight dates) to the given date. Sorry I'm just a beginner in sql.
In response to sir Akina
I don't have the actual code yet because it confuses me of what to do first.
- Should I make a temporary table, loop the result of the query and filter each of the result (but how to get the 2 closest dates out of eight dates) then insert it to the temporary table?
- or should I put all the condition under the
gendate1 = '1/6/2019' ,gendate2 = '4/6/2019' ,gendate3 = '2/6/2019' ,gendate4 = '3/6/2019' ,gendate5 = '5/6/2019' ,gendate6 = '7/6/2019' ,gendate7 = '8/6/2019' ,gendate8 = '10/6/2019' @date_start = '1/30/2019'
How to get the
gendate3? (they are the closest to