MySQL Datetime Less Than Functionality

dmcmulle

I have a table with some datetime columns. I need to be able to select the rows between two datetimes on the same column.

For example:

SELECT *
FROM `t_punchcards`
WHERE UserID = 'root' AND
      PunchInTime > CAST('01-01-2015 00:00:00' AS DATETIME) AND
      PunchInTime < CAST('03-06-2015 23:59:59' AS DATETIME);

For some strange reason, this yields no results. I have tried using BETWEEN, I've tried not casting to datetime from string (just use a raw string), I've tried <= >=. I'm very lost.

As a sidenote, removing the second constraint (AND PunchInTime < CAST('03-06-2015 23:59:59' AS DATETIME); WILL in fact yield results. Are you not allowed to do a comparison against the same column in the same query?

Here's an example of the table:

UserID(varchar)      PunchInTime(datetime)    PunchOutTime(datetime)
root                 01-01-2015 8:02:31       01-01-2015 12:35:51
Gordon Linoff

Just write this logic as:

WHERE UserID = 'root' AND
      PunchInTime > DATE('2015-01-01') AND
      PunchInTime < DATE('2015-03-07')

Note that I removed the time component from the second value. This is a cleaner way of making the comparison, unless you really want to treat the last few milliseconds of a day differently from the rest of the time during the day.

Note: If you are not using MySQL or you want more compliant code, the following works in more databases:

WHERE UserID = 'root' AND
      PunchInTime > CAST('2015-01-01' as DATETIME) AND
      PunchInTime < CAST('2015-03-07' as DATETIME)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mySQL less than one hour

From Dev

Query MongoDb for a datetime value less than NOW

From Dev

Python: Search for DateTime inside a Dictionary that is Less than a certain DateTime

From Dev

datetime.datetime.utcfromtimestamp does not support dates less than 1970

From Dev

MYSQL SELECT only if rows are less than 5

From Dev

Optimize MySQL indexes to query in less than a second

From Dev

MySQL WHERE Clause - Age Greater/Less than

From Dev

extract less than the limit value mysql

From Dev

MySQL query with less than and ORDER BY DESC

From Dev

Optimize MySQL indexes to query in less than a second

From Dev

MYSQL less Than function not working properly

From Dev

SQL get max record that is less than or equal to a datetime paramter

From Dev

Crystal Report DateTime field shows one day less than it should

From Dev

SQL datetime less than NOW() returns invalid results

From Dev

Set array to NAN if less than x amount of data points are in a datetime

From Dev

MySQL Where DateTime is greater than today

From Dev

Mysql Select datetime, greater than timestamp

From Dev

Compare datetime of rows with same ID, return only columns with datetime difference less than 4 hours

From Dev

MySQL select 0 if difference is less than 0, otherwise select difference

From Dev

MySQL - Add 0's to numbers with less than 9 digits

From Dev

MySQL "less than" operator doesn't bring correct results

From Dev

Fetch name when number of distinct names less than 200 mysql

From Dev

MySQL to select a daterange where sum is less than a value

From Dev

Mysql - Select rows which have less than x "child rows"

From Dev

Fetch name when number of distinct names less than 200 mysql

From Dev

MYSQL Select all data except negative numbers or less than 0

From Dev

mysql slow query when results are less than limit

From Dev

MYSQL get all the results of a date less than given value

From Dev

Query to get values of one table which are not there in string column of another table, also less than a particular datetime value

Related Related

  1. 1

    mySQL less than one hour

  2. 2

    Query MongoDb for a datetime value less than NOW

  3. 3

    Python: Search for DateTime inside a Dictionary that is Less than a certain DateTime

  4. 4

    datetime.datetime.utcfromtimestamp does not support dates less than 1970

  5. 5

    MYSQL SELECT only if rows are less than 5

  6. 6

    Optimize MySQL indexes to query in less than a second

  7. 7

    MySQL WHERE Clause - Age Greater/Less than

  8. 8

    extract less than the limit value mysql

  9. 9

    MySQL query with less than and ORDER BY DESC

  10. 10

    Optimize MySQL indexes to query in less than a second

  11. 11

    MYSQL less Than function not working properly

  12. 12

    SQL get max record that is less than or equal to a datetime paramter

  13. 13

    Crystal Report DateTime field shows one day less than it should

  14. 14

    SQL datetime less than NOW() returns invalid results

  15. 15

    Set array to NAN if less than x amount of data points are in a datetime

  16. 16

    MySQL Where DateTime is greater than today

  17. 17

    Mysql Select datetime, greater than timestamp

  18. 18

    Compare datetime of rows with same ID, return only columns with datetime difference less than 4 hours

  19. 19

    MySQL select 0 if difference is less than 0, otherwise select difference

  20. 20

    MySQL - Add 0's to numbers with less than 9 digits

  21. 21

    MySQL "less than" operator doesn't bring correct results

  22. 22

    Fetch name when number of distinct names less than 200 mysql

  23. 23

    MySQL to select a daterange where sum is less than a value

  24. 24

    Mysql - Select rows which have less than x "child rows"

  25. 25

    Fetch name when number of distinct names less than 200 mysql

  26. 26

    MYSQL Select all data except negative numbers or less than 0

  27. 27

    mysql slow query when results are less than limit

  28. 28

    MYSQL get all the results of a date less than given value

  29. 29

    Query to get values of one table which are not there in string column of another table, also less than a particular datetime value

HotTag

Archive