Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values before certain date in sql

Tags:

date

sql

multirow

i have one question about get multi rows before the date getting infection:

patientID   Appt_reason dateofProcedure
1                        1/2/2016
1                        1/3/2016
1             infectoin  1/4/2016
2                        3/1/2016
2                        3/3/2016
3                        3/4/2016
3             infectoin  3/5/2016
3                        3/6/2016
3                        3/7/2016
5                        2/2/2016
5             infectoin  2/3/2016
5                        2/4/2016   

I need to get rows like this

patientID   Appt_reason dateofProcedure
1                       1/2/2016
1                       1/3/2016
1          infectoin    1/4/2016
3                       3/4/2016
3          infectoin    3/5/2016
5                       2/2/2016
5          infectoin    2/3/2016

anybody know the answer? thanks

like image 680
joan Avatar asked Nov 04 '16 03:11

joan


1 Answers

SELECT
    patientID,
    Appt_reason,
    dateOfProcedure
FROM
    tableName
WHERE
    dateOfProcedure < '2016-04-03'

Note I'm using ISO-8601 format which is unambiguous and supported by all modern database systems.

Note that it's using the < (less-than) operator instead of <= (less-than-or-equal to) because in SQL a date literal value can represent datetime values, so '2016-02-01' >= '2016-02-01 01:00' is actually false, because '2016-02-01' has a hidden time component equal to midnight, and 1am is after midnight.

like image 157
Dai Avatar answered Oct 18 '22 03:10

Dai