mysql date range ignores end date in result - mysql

I am not getting result till '2013-07-31' instead I get one day before endDate.
How can I add one day in '2013-07-31'
select * from employee
where admissiondate>='2013-01-01'
and admissiondate<='2013-07-31'

2013-07-31 12:00:00 is neither smaller nor exactly equal to 2013-07-31.
To fix it either you strip off the time part from your datetime column using date()
select * from employee
where date(admissiondate) between '2013-01-01' and '2013-07-31'
But that won't make use of indexes. Or add a time part like this
select * from employee
where admissiondate >= '2013-01-01'
and admissiondate <= '2013-07-31 23:59:59'
SQLFiddle demo

Use the DATE_ADD() function ...
MySQL DOC

Related

DateTime condition [duplicate]

I get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.

Using MySQL how do I select data between two dates if the dates are in separate columns

In my database, I have a column with a check-in date and a column with a check-out date. I need to select every row that has a check-in date <= 7/30/2017 and a check-out date that is >= 7/30/2017.
This is the code I have now:
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= '7/30/2017'
AND cast(checkout as date) >= '7/30/2017'
Here is an example date from the DB:
2018-09-18
When I run this query, I do not get any results, but I know that I have a check-in date equal to 7/30/2017. What am I missing? Or is there an easier way to accomplish my goal?
Assuming that you are casting valid values for date
You should convert also the literal the date properly
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= str_to_date('7/30/2017' , '%d/%m/%Y')
AND cast(checkout as date) >= str_to_date('7/30/2017' , '%d/%m/%Y')
and you can also use between
SELECT *
FROM `v_reservation_records`
WHERE str_to_date('7/30/2017','%d/%m/%Y')
between cast(checkin as date) AND cast(checkout as date)
Try like this
SELECT *
FROM `v_reservation_records`
WHERE DATE_FORMAT(checkin, '%m/%d/%Y') between '7/30/2017'
AND '7/30/2017'

MySQL Where DateTime is greater than today

I want to get every record from my MySQL database which is greater than today.
Sample:
"Go to Lunch","2014-05-08 12-00-00"
"Go to Bed","2014-05-08 23-00-00"
Output should only:
"Go to Bed","2014-05-08 23-00-00"
I use the DateTime for the Date Column
Already searched:
MySQL Where date is greater than one month?
Datetime equal or greater than today in MySQL
But this does not work for me.
QUERY(FOR PHP):
SELECT `name`,`date` FROM `tasks` WHERE `tasks`.`datum` >= DATE(NOW())
OR (FOR PhpMyAdmin)
SELECT `name`,`date` FROM `tasks` WHERE `tasks`.`datum` >= 2014-05-18 15-00-00;
How can I write the working query?
Remove the date() part
SELECT name, datum
FROM tasks
WHERE datum >= NOW()
and if you use a specific date, don't forget the quotes around it and use the proper format with :
SELECT name, datum
FROM tasks
WHERE datum >= '2014-05-18 15:00:00'
I guess you looking for CURDATE() or NOW() .
SELECT name, datum
FROM tasks
WHERE datum >= CURDATE()
LooK the rsult of NOW and CURDATE
NOW() CURDATE()
2008-11-11 12:45:34 2008-11-11
SELECT *
FROM customer
WHERE joiningdate >= NOW();

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

Select mysql query between date?

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'