dividing datetimes by date - mysql

I save my news in db and each news have an field that save it's datetime in format:
xxxx-xx-xx xx:xx:xx
and now I want retrieve news by date xxxx-xx-xx and for example today what I must write in where of my sql code

It's very simple:
SELECT *
FROM tableName
WHERE DATE( datetime_field ) = DATE( NOW( ) )
please notice that DATE() return only date value and NOW() return current datetime

use DATE function. eg
SELECT *
FROM tableName
WHERE DATE(columnName) = '2012-12-20'
this will retrieve all records where date is December 20, 2012.

select *
from TableName
where
DATE_FORMAT(columnName,'%m-%d-%Y') = '01-01-2012'

Related

SQL - Get result of current year only

How can I get the result of the current year using SQL?
I have a table that has a column date with the format yyyy-mm-dd.
Now, I want to do select query that only returns the current year result.
The pseudo code should be like:
select * from table where date is (current year dates)
The result should be as following:
id date
2 2015-01-01
3 2015-02-01
9 2015-01-01
6 2015-02-01
How can I do this?
Use YEAR() to get only the year of the dates you want to work with:
select * from table where YEAR(date) = YEAR(CURDATE())
Using WHERE YEAR(date) = YEAR(CURDATE()) is correct but it cannot use an index on column date if exists; if it doesn't exist it should.
A better solution is:
SELECT *
FROM tbl
WHERE `date` BETWEEN '2015-01-01' AND '2015-12-31'
The dates (first and last day of the year) need to be generated from the client code.
When I tried these answers on SQL server, I got an error saying curdate() was not a recognized function.
If you get the same error, using getdate() instead of curdate() should work!
--========= Get Current Year ===========
Select DATEPART(yyyy, GETDATE())
SELECT id, date FROM your_table WHERE YEAR( date ) = YEAR( CURDATE() )
SELECT
date
FROM
TABLE
WHERE
YEAR (date) = YEAR (CURDATE());
If the date field contains a time component, you want to include December 31 so you have to go to January 1 of the next year. You also don't have to use code to insert dates into the SQL. You can use the following
SELECT * FROM table
WHERE date BETWEEN MAKEDATE(YEAR(CURDATE()), 1) AND MAKEDATE(YEAR(CURDATE())+1, 1)
This will give you January 1st of the current year through January 1st at midnight of the following year.
As #Clockwork-Muse pointed out, if the date field does not contain a time component, you would want to exclude January 1 of the following year by using
WHERE date >= MAKEDATE(YEAR(CURDATE()), 1) AND date < MAKEDATE(YEAR(CURDATE())+1, 1)
You can do this using SQL DATE_FORMATE(). like below:
SELECT
date
FROM
TABLE
WHERE
DATE_FORMAT(date, '%Y') = YEAR (CURDATE());
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MAX(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
/*
This will find the newest records in the table regardless of how recent the last time data was entered.
To grab the oldest records from the table do this
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MIN(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
*/

Mysql select users which have birthday current month

I have stored in the database in a varchar column the birthdays like this
dd/mm/YYYY
How can I select the birthday people from the current month directly from MySQL query??
And show using PHP
Thanks
First, do not store dates as a VARCHAR. Convert it to a DATE.
Once that's fixed, use one of the many MySQL date time functions:
SELECT * FROM users WHERE MONTH(birthday) = MONTH(NOW());
SELECT
*
FROM
yourtable
WHERE
MONTH(STR_TO_DATE(yourdatefield, '%d/%m/%Y')) = MONTH(NOW())
Assuming date is stored in %m/%d/%Y this format you can change this format according to your need.
and %m we are selecting only the month and comparing it to the current month MONTH(NOW()).
Replace DOB by your column and table by your table name
select * from table
where date_format(str_to_date(DOB, '%m/%d/%Y'), '%m') = MONTH(NOW());;
You should change your column type to DATE. e.g.
ALTER TABLE `people` CHANGE `dob` `dob` DATE NOT NULL;
By doing so you can then use the MySQL query date functions to filter the results.
SELECT * FROM people WHERE MONTH(dob) = MONTH(CURDATE()) AND YEAR(dob) = YEAR(CURDATE())
For Get User list whose birthday in current month in mysql if field datatype is date
$Toaday = '%'.date('-m-').'%';
$query = " select * from client where birth_date LIKE '$Toaday' ";
In your case declare $Today = '%'.date('/m/').'%';
As you used a VARCHAR field instead a DATE field,
you have to cast the value and then use the normal date functions. like
SELECT *
FROM table_name
WHERE MONTH(CAST(col_name AS DATE)) = MONTH(NOW());
I have store dates as a timestamp in tables so I create a query like this. and it's working fine.
don't use varchar for a date.
select first_name, last_name, date_format(FROM_UNIXTIME(`dateofbirth`), '%m/%d/%Y') as dob from users where date_format(FROM_UNIXTIME(`dateofbirth`), '%m') = MONTH(NOW())

how to search current date in mysql database that store standard date as ('Y-m-d H:i:s')?

I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()

SQL SELECT * FROM datetime = CURDATE() return an empty result set

Can anyone tell me where's my mistake? By using that query, it should return some rows where the data have datetime = '2012-10-12' right? Here is my reference
My datetime column = 'YYYY-MM-DD HH:MM:SS', data type = datetime.
I am using XAMPP v1.8.0, MySQL v5.5.25a.
Try CASTing datetime to date by using DATE()
SELECT *
FROM tableName
WHERE DATE(`datetime`) = DATE(CURDATE())
YYYY-MM-DD HH:MM:SS is not equal to YYYY-MM-DD
2012-01-01 12:12:12 is not equal to 2012-01-01 00:00:00
Do not use functions on your columns, e.g. DATE(datetime) - mysql can't use your index.
It's almost certainly better to use a range:
WHERE `datetime` between '2012-01-01 00:00:00' and '2012-01-01 23:59:59'
or store just the DATE portion in a separate column (which will have lower cardinality and be better treated by the optimizer).
SELECT * FROM tableName WHERE DATE(datetime) = CURDATE()
use DATE() from mysql to format the TIMESTAMP to DATE
Select * FROM tablename WHERE DATE(colname) = CURDATE()

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.