SQL query to get records from same day - mysql

I wish to find all records of the current day.
I have a field Date of type DATE.
If I use
WHERE `Date` = '2011-04-07'
it works
but if I use:
WHERE `Date`='CURDATE()'
or
WHERE `Date`='NOW()'
it does not return any results (when there actually are some).
How do I get the current date in the right format to use it in my SQL query?
I am using MySQL
And the date was originally entered in the database using NOW().

Use
WHERE `Date`=CURDATE()
The quotes (') are used to wrap up a string (text).
Edit: I can see now that you say the value was stored with NOW() : it probably includes a time element too. Will update answer imminently..
This will compare the date part of the Date field to today's date:
WHERE DATE(`Date`)=CURDATE()

Related

Remove unnecessary string and Update date format in MySql

Basically my date in MySql table looks like this
2001-04-16
The format is actually day-month-year. Unfortunately 20 is added to the date. So finally i should update this date. This should become
01-04-2016
How to update my all date format in my table
Don't change this in the database, change it when you need to display it. The ISO date format is what's used internally and should be kept that way, it's the most native, efficient format, and it sorts correctly.
You can select them out:
SELECT DATE_FORMAT(date_column, '%d-%m-%Y') FROM table_name
Using the DATE_FORMAT() function.
Even better is to do this in your application layer, whatever that is, using a date formatting function tuned to the user's locale.
If you've got a bunch of bad data in your database you need to convert, you can re-write it with the DATE_FORMAT() function to strip out the mistakes.
For example:
UPDATE table_name SET date_column=DATE_FORMAT('20%d-%m-%y', date_column)
For mysql, MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. so you can fetch date in whatever format you want from database, please see https://dev.mysql.com/doc/refman/5.6/en/datetime.html for more info.
you can use DATE_FORMAT() function :
SELECT DATE_FORMAT('%d-%m-%Y', date) as desired_date FROM table_name where 1

UNIX_TIMESTAMP outputting NULL in MySQL?

I've got a table setup which has populated data. In column "date", I have dates in the following format:
yyyymmdd i.e. 20131110
I have created a new field and called it newdate with the text format.
Then, I open up the SQL window and put the following in
UPDATE wl_daily
SET
newdate = UNIX_TIMESTAMP(date)
For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason
Any suggestions?
That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.
Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.
While John Cronde's answer is correct - it doesnt help your situtation
UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))
will do the conversion for example
SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d'))
returns
unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))
---------------------------------------------------
1384128000
You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production

why this MySQL SELECT doesn't include the right dates?

The main problem is, that I have stored in database datetime , not the date (what I need). Ok never mind.
I have thousands of reports stored each day.
I need to LEFT by 10 my datetime_view (to cut the time) and everything's fine. Except this. I'm trying to figure out why do I have to put in the condition + one day from the future? Otherwise it won't search what I want.
SELECT
LEFT(datetime_view,10),
count(type)
FROM reports
WHERE
type IN (1,2,5)
AND
datetime_view>='2012-10-28'
AND
datetime_view<='2012-11-04'
group by LEFT(datetime_view,10);
You can see I must search from the future. Why??
It gives me an output from 28.10 to 3.11 ....
don't use string operations on date/time values. MySQL has a huge set of functions for date/time manipulation. Try
GROUP BY DATE(datetime_view)
instead, which will extract only the date portion of the datetime field. Your string operation is not y10k compliant. Using the date() function is.
As for your plus one day, consider how the comparisons are done: A plain date value, when used in date/time comparisons, has an implicit 00:00:00 time value attached to it, e.g. all dates have a time of "midnight".
i think it's better to use DATE(datetime_view) to cut the time instead of LEFT(datetime_view,10), also on the where condition:
DATE(datetime_view) <= '2012-11-03'

How to use MYSQL on dates formatted as MM/dd/yy

I have a database full of dates that are in the format: MM/dd/yy. For example, today's date (November 4, 2011) is saved in the database as: 11/04/11. I'm having trouble with date ranges since my dates aren't formatted as yyyy/MM/dd (example: 2011/11/04). I don't have a way to change the way the database is populated, so I need to account for the date formatting differences within my MYSQL queries (via VB.NET). Here is my query (it doesn't work well because the dates are in the incorrect format):
SELECT CMP_DATE FROM my_data WHERE OBJ_DATE >= '1994/01/01' AND CMP_DATE <= '2011/11/04'
Is there anyway to reformat the dates within the query? Thank you.
The function you need is STR_TO_DATE:
SELECT CMP_DATE
FROM my_data
WHERE STR_TO_DATE(OBJ_DATE, '%d/%m/%y') >= '1994-01-01'
AND STR_TO_DATE(CMP_DATE, '%d/%m/%y') <= '2011-11-04'
#Piskvor has a good point, too, you should consider interpreting the 2-digit year yourself, instead of letting mysql perform some arbitrary conversion.

Mysql time stamp queries

I have a column that uses time stamp. My question is I am a bit confused about how to make queries against it,how would I say
Where $time is after X date
Are queries made in local time or CUT?
When I just try to do where andthe post date /time I get an error because of the space and if I quote it I think it takes it as a string : /
What format do I use for the date in the WHERE clauss !?!?
You can use the normal logical comparisons, for example:
SELECT * FROM table WHERE date >= '2010-01-31'
SELECT * FROM table WHERE date >= '2010-01-31 12:01:01'
Time is generally in your current local time, but you can run the query "SELECT CURTIME()" to check. Also, make sure you have the year-month-date in the right order... that can cause issues.
The manual has more details:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Assuming you talk about TIMESTAMP column type (not Unix timestamp) the format is either 'YYYY-MM-DD HH:MM:SS' (quoted) or YYYYMMDDHHMMSS
Atually, in the first case you can use any spearators you wish (or none), only numbers are taken into ccount