Formatted date range - return of between

Asked

Viewed 36 times

0

I am doing a select of a range of dates, however, when I do the formatted is returning data that should not return, in case, more months and less months... Follows interval and query.

SELECT
    id, DATE_FORMAT( created_at,'%d/%m/%Y %H:%i:%s' ) as created_at
FROM
    use_card 
WHERE
    DATE_FORMAT(created_at, '%d/%m/%Y %H:%i:%s') BETWEEN "01/05/2019 00:00:00" 
    AND "13/06/2019 18:05:32" 
ORDER BY
    DATE_FORMAT(created_at, '%d/%m/%Y %H:%i:%s') ASC

id  created_at
15  02/04/2019 09:42:47
13  02/05/2019 09:40:50
17  02/05/2019 09:43:41
80  03/06/2019 19:48:13
117 04/06/2019 07:42:42
116 04/06/2019 08:20:21
118 04/06/2019 08:45:24
122 06/02/2019 11:58:42
119 06/06/2019 11:57:26
120 06/06/2019 11:57:57
121 06/06/2019 11:58:33
123 06/06/2019 18:07:20
124 06/06/2019 18:08:36
125 06/06/2019 19:27:38
126 06/06/2019 22:09:15
25  07/05/2019 10:07:26
27  07/05/2019 10:17:17
28  07/05/2019 10:19:08
29  07/05/2019 10:45:38
26  07/07/2019 10:14:03
57  09/05/2019 09:40:20

If anyone can help, I’d appreciate it.

1 answer

1


It turns out that the function DATE_FORMAT returns a string. Do not use this function to filter or sort, it does not make sense something like this.

The select should be

SELECT
    id, DATE_FORMAT(created_at, '%d/%m/%Y %H:%i:%s') as created_at
FROM
    use_card 
WHERE
   created_at BETWEEN "2019/05/01 00:00:00" AND "2019/06/13 18:05:32" 
ORDER BY
    created_at ASC

See working on Dbfiddle

  • If there is any possibility of misinterpreting the given deadlines use BETWEEN CAST("01/05/2019 00:00:00" AS DATETIME) AND CAST("13/06/2019 18:05:32" AS DATETIME). Note that the LINQ response eliminates any problems of date interpretation when using the ISO format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.