select rows based on todays date - mysql

I am trying to select records from a database only if they match today's date. The format for the date in the database is 2012-06-20 9:30:00 I am using the statement SELECT id FROMnewsreportsWHERE DATE(newsdate) = CURDATE() but it doesn't not return any records for today?
Screenshot of column with dates
http://img135.imageshack.us/img135/8399/2347f03df0394cd898c7fc5.png

DATE_FORMAT(NOW(),'%Y-%m-%d') = FORMAT_DATE(NOW(newsdate), '%Y-%m-%d')
Or better:
DATE(newsdate) = DATE(NOW())
The best way is to store the additional column with 2001-09-11 date format and compare this one
Example, thanks to #Conrad Frix

It looks like the curdate function is going to give you something different than the date format you've got in the database.
Take a look at the docs here
MySQL Date and Time Functions
It's supposed to return a date in this format:
2008-11-11
So, you could either search for a date range between curdate() and curdate() + INTERVAL 1 DAY (untested), or store the dates in the curdate() format.

Related

SQL between date with dd.mm.yyyy only matches on day

I want to use a Datepicker to select specific entries in my Database.
The Problem i face is that the entries i get as a reposnse from my query are only sorted for the day part of the date.
I use the date format (varchar field in database):
*dd.mm.yyyy hh:mm ($date = date("Y-m-d") . ' ' . date("H:i");)*
For example:
01.04.2016
10.04.2016
11.04.2016
01.04.2017
10.04.2017
11.04.2017
The Query to get the entries:
SELECT *
FROM order_date
WHERE date >= '10.04.2017' AND date <= '10.04.2017'
Now i expected to get only the to matching entries but
the result of the Query are all four table entries which start with day 10 or 11. Even two of them are in the year 2016.
As i saw in other posts the between XX and YY work if the date is set with yyyy-mm-dd or yyyymmdd but the problem is that i got a database with around 20k entries on which i need a datepicker so i would prefer to use the givin format of the date without rewriting all entries.
Is there a possibilty to get a datepicker between with dd.mm.yyyy format aswell?
Any help highly appriciated
Try something like this,
STR_TO_DATE('10.04.2017', '%d.%m.%Y')
SELECT *
FROM order_date
WHERE date >= STR_TO_DATE('10.04.2017', '%d.%m.%Y') AND date <= STR_TO_DATE('10.04.2017', '%d.%m.%Y')
You can typecast the column as DATE, and use the ORDER BY clause.
Use STR_TO_DATE function may help.
STR_TO_DATE('10.04.2017','%d.%m.%Y')
How about something around this idea ?
SELECT *
FROM Mytable WHERE date
BETWEEN CONVERT(datetime,'10.04.2017',104 )
AND CONVERT(datetime,'10.04.2017',104 )

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'

sql query with unixtime

I am trying to select everything from my table with today's date. But I found that that date column is in unix time stamp. So, how do I select everything with today's date? or for example only yesterday's ? If it was normal date instead of unix time it would be easy, but... here is what I have put for my query so far..
$ann_renewal_query = "SELECT * FROM annual_renewal WHERE due_date '%$today%' order BY due_date ASC";
You just need to convert the values to the unix timestamp version. The best way is to convert the current time to Unix timestamp:
where due_date >= UNIX_TIMESTAMP(curdate()) and
due_date < UNIX_TIMESTAMP(date_add(curdate(), interval 1 day)
This version of the query allows it to take advantage of an index on due_date.

MySQL date interval with specific date format

I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.

datetime searching using sub-sections of the YYYY-MM-DD HH:MM string

[MySQL/PHP] My table has a date column of datetime format. All records are of the YYYY-MM-DD HH:MM:SS variety.
MySQL queries like SELECT record FROM table WHERE date > '1941' AND date < '1945' work nicely.
MySQL queries like SELECT record FROM table WHERE date > '1941-03-01' AND date < '1945-01-30' also work nicely.
But what about if I wanted all records that were filed in March, regardless of year? Or all records filed on the 17th, regardless of month/year?
``SELECT record FROM table WHERE date = '03'` clearly doesn't work.
I know I could snag it with a LIKE '%-03-%' parameter, but that doesn't leave room for me to search for range, like all records from March to May.
Help? :-)
Try WHERE MONTH(DATE(`date`)) BETWEEN '03' AND '05'
The DATE() part is to extract the date from the timestamp to be used with MONTH().
You can use MySQL date functions:
SELECT record FROM table WHERE MONTH(date) = 3
SELECT record FROM table WHERE DAY(date) = 17
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
If you look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, you will find many useful functions for your purpose.
... WHERE MONTH(date) = 3, e.g. =)