I have a DATETIME column on my table which stores when a record was created. I want to select only the records created on a particular date. If I try:
SELECT *
FROM myTable
WHERE postedOn = '2012-06-06'
It returns no rows even though there are many rows in the table with the postedOn set as
2012-06-06 21:42:02, 2012-06-06 07:55:17 , and so forth.
Any ideas?
Use the DATE scalar:
SELECT *
FROM myTable
WHERE date(postedOn) = '2012-06-06'
SELECT *
FROM myTable
WHERE DATE(postedOn) = '2012-06-06'
DATE() returns the date part of a datetime field.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
Create a time range by adding a day to the date:
SELECT *
FROM myTable
WHERE postedOn >= '2012-06-06' and postedOn < '2012-06-07'
This is the most efficient way, as the query can use an index on the field.
Related
I have a table: box (id autoincrement, net_amount, created_at timestamp);
I need to create a query in php mysql to select the last inserted record every month. Thus to get the net_amount at the end of every month.
I am trying this simple query:
select * from box
where box.created_at < 20055332516028
While the max created_at in my table is 2017-10-14 10:42:30, there is no records when I use the given query, I need to increase the number to get the records!!!
20055332516028 is not a Unix timestamp. You need to get timestamp of the end of the previous month, something like this:
$date = new DateTime();
$date->setDate($date->format('Y'),$date->format('m'),1);
$date->setTime(0,0,0);
$date->sub(new DateInterval('PT1S'));
$endOfMonth = $date->getTimestamp();
and then use it in a query:
select * from box where box.created_at < unix_timestamp(?) order by box.created_at desc limit 1
if i understand your problem your looking for this:-
SELECT b1.*
FROM box b1 LEFT JOIN box b2
ON (b1.name = b2.name AND b1.id < b2.id)
WHERE b1.created_at < PUT YOUR DATE and b2.id IS NULL;
Hope it helps
I have a table in my database like this :
id date origine
1 2015-12-04 16:54:38 1
Now I want to get only data witch have the date = 2015-12-04. So I tried like this :
select * from table where id = 1 and date = "2014-12-04"
But I have no data. Can you help me please ?
You can use the date function:
where id = 1 and date(date) = '2015-12-04'
However, for performance reasons, it is often better to use inequalities. This allows MySQL to use an index on id, date for the query:
where id = 1 and
(date >= '2015-12-04' and date < date_add('2014-12-04', interval 1 day))
you can use Date Function of mysql which returns date from DateTime or truncate the Time part
select * from table where id = 1 and Date(date) = "2014-12-04"
There are several date related function out there you can use, take the following:
select *
from table
where id = 1
and date_format(date, '%Y-%m-%d') = '2015-12-04';
date_format will format your date column to a particular format.
In MSSQL you can simply say
SELECT *
FROM Table
WHERE ID = 1
AND Date > '2015-12-04'
I'm not familiar with mysql, but I assume something similar would work here. This date gets formatted as 2015-12-04 00:00:00 so in effect it matches everything with a date of 2015-12-04 and a time greater than 00:00:00.
If you happen to have rows with time of 00:00:00, just use >= instead.
I am trying to pass the date as a where condition in select query.but in the database the field is in datetime format.I don't know how to pass the condition?
SELECT * FROM (`scl_students`) WHERE `std_added_on` = '2015-03-03'
i got it.
SELECT * FROM (`scl_students`) WHERE DATE(std_added_on) = '2015-03-06'
I guess the problem is that the std_added_on contains time portion which could be non-zero. In order to select all rows for a given date you would write:
SELECT *
FROM `scl_students`
WHERE `std_added_on` >= '2015-03-03'
AND `std_added_on` < '2015-03-03' + INTERVAL 1 DAY
This is better than DATE(`std_added_on`) = ... performance wise.
You convert datetime to date after u try to run query
Use that query as this.
SELECT * FROM (scl_students) WHERE std_added_on = ('2015-03-03');
The datetime format expects that you parse time value also.
Try doing the following code.
SELECT * FROM (scl_students) WHEREstd_added_on= '2015-03-03 00:00:00'
Above query will only if your std_added_on value stored in database is = 2015-03-03 00:00:00
I have a datetime column that i'm trying to compare with a date value in the DD/MM/YYYY format.
I'm trying this:
SELECT * FROM TABLE WHERE STR_TO_DATE(DATETIME_COLUMN, '%d/%m/%Y') = '04/04/2014'.
It doesn't work.
Even tried this:
SELECT * FROM TABLE WHERE DATETIME_COLUMN = '04/04/2014'
Which seems to be working on the rest of my code, but it doesn't.
Is there another function?
You can achieve your goal using this query which will keep you predicate sargable.
SELECT * FROM TABLE WHERE DATETIME_COLUMN = STR_TO_DATE('04/04/2014', '%d/%m/%Y')
if your DATETIME_COLUMN column has time part and you want to get all dates within given date, use this:
SELECT * FROM TABLE
WHERE DATETIME_COLUMN >= STR_TO_DATE('04/04/2014', '%d/%m/%Y')
AND DATETIME_COLUMN < STR_TO_DATE('05/04/2014', '%d/%m/%Y')
Use like this
SELECT * FROM TABLE WHERE DATE_FORMAT(DATETIME_COLUMN,'%d/%m/%Y') = '04/04/2014';
I am trying to only grab records that fall in a certain date range. The problem is that the timestamp and the date are stored as a string in the same cell. I want to only grab rows with a date that falls betweed 2013-05-01 and 2013-05-03.
date (stored as string)
2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17
mysql
SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03
Try
SELECT *
FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'
SQLFiddle
As #FreshPrinceOfSO absolutely correctly noted no index will be used in that case
SELECT * FROM table1
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y')
BETWEEN '2013-05-01' AND '2013-05-03'
The string is almost valid syntax for a datetime. Thus, an alternative, if perhaps slower method is to replace the 'T' with a space and then cast it to a datetime.
SELECT * FROM table1 WHERE
CAST(REPLACE(`date`, 'T', ' ') AS DATETIME)
BETWEEN '2013-05-01' AND '2013-05-03';
SELECT * FROM table
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')