DateTime condition [duplicate] - mysql

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.

Related

Query MYSQL with a date in the format of 10-OCT-15

I am trying to query a MYSQL database to return all records with today's date -
SELECT *
FROM credit_application
created_on = '15-OCT-15';
But it's failing because of the 'OCT' part within the query. How can I resolve this please?
Use mysql DATE_FORMAT function
SELECT *
FROM credit_application
DATE_FORMAT(created_on,'%d-%b-%y') = '15-OCT-15';
Use mysql STR_TO_DATE function
SELECT *
FROM credit_application
created_on = STR_TO_DATE('15-OCT-15', '%d-%b-%y');
Of course, this assumes your created_on field is just a DATE. If it's actually a DATETIME or TIMESTAMP, then you'll need to do a range query instead:
SELECT *
FROM credit_application
created_on >= STR_TO_DATE('15-OCT-15', '%d-%b-%y') AND
created_on < DATE_ADD(STR_TO_DATE('15-OCT-15', '%d-%b-%y'), INTERVAL 1 DAY);
The other suggestions of using DATE_FORMAT would require the function to be applied to every row in the table, preventing use of any index you might have. It would be a non-sargable query.

How to compare a date when the date is stored as a varchar

The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'

mysql date range ignores end date in result

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

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

how to test date and datetime with mysql

My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.
my code is only :
SELECT *
FROM table
WHERE date = '$date'
ORDER BY score DESC
with
$date = date("Y-m-d");
well, as expected it doesnt work :| you guys have a solution here ?
Following from Pascal Martin, you could extract the date part from the date+time field:
SELECT * FROM table WHERE DATE(date) = '2009-12-19'
Source: MySQL - Date and Time Functions
Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)
Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".
In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".
A solution is to compare like this :
select *
from table
where date >= '2009-12-19'
and date < '2009-12-20'
Which will be interpreted as :
select *
from table
where date >= '2009-12-19 00:00:00'
and date < '2009-12-20 00:00:00'
And, if you don't want to do the math to get the date of the following date, you can use the adddate function :
select *
from table
where date >= '2009-12-19'
and date < adddate('2009-12-19', interval 1 day)
So, in your case, something like this should do the trick :
select *
from table
where date >= '$date'
and date < adddate('$date', interval 1 day)
order by score desc
You probably want to format the data when you select it:
SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.