I want to write a query to find all the items in my 'schedule_items' table that match a YYYY-MM-DD value of a DATETIME field.
something like: SELECT * FROMschedule_itemsWHEREstart?matches? '$date'
Where start would be a DATETIME field with data like 2009-09-23 11:34:00 and $date would be something like 2009-09-23
Can I do this or do I need to load all the values and then explode and compare the values in my DATETIME start field?
You can use LIKE in MySQL
SELECT * FROM schedule_items WHERE start LIKE 'some-date%'
You put a % sign as a wildcard.
Be aware though that like can be a very heavy query. Specially if you use % at the start of a string.
If you are working with dates, like it seems you are saying :
Where start would be a DATETIME field
with data like 2009-09-23 11:34:00 and
$date would be something like
2009-09-23
Using LIKE might not be the only/best solution : there are functions that deal with dates ; and you probably can use comparison operators too.
In your case, you can probably use something like this :
select *
from headers_sites
where date_fetch >= '2009-07-15'
limit 0, 10
Of course, you'll have to adapt this query to your tables/fields ; something like this might do, I suppose :
SELECT * FROM schedule_items WHERE start >= '$date'
This will get you every data for which the date is more recent than $date.
If you only want the date of one day, this could do :
SELECT *
FROM schedule_items
WHERE start >= '$date'
and start < adddate('$date', interval 1 day)
This might be better than "like", if you start column has an index -- not sure, though ; but, still, what you are willing to get will be obvious from your query... And that is nice.
in SQL you can use LIKE instead of = and then you get the % wildcard. Example:
select ... where start like "2009-09-%";
(That would match anything from August 2009)
If it is needed to extract records of a date from datetime field, you need to use following format:
SELECT * FROM schedule_items WHERE date(start) = 'some-date time'
Related
I'm trying to compare 2 dates with an SQL Query,
My query is this :
"SELECT table.toto
FROM table
WHERE
table.date < $date"
$date is a variable that comes from my PHP code
In my table, my date is formatted like this : dd/mm/YYYY
Unfortunately, it doesn't work as expected, it returns only one result (it's supposed to return way more results), which date is : 1312-09-15 00:00:00 but in database, the date is formatted like dd/mm/YYYY.
Well, I found a solution to my problem.
I don't know why but I just have set the "$date" format to : n/d/Y
and to wrap it between "#" in code like this :
"SELECT table.toto
FROM table
WHERE
table.date < #date#"
In my database field there in date and time saved using time(); function.
I want to compare that one with given date in date format like '20/02/2015 '
my query is like:
select view,optin,insertTime,isUnique from sg_page_report as PR where insertTime='20/02/2015'"
where insertTime contains different format date like '1393587636'
How can I resolve this one?
WHERE FROM_UNIXTIME(insertTime)='2015-02-20'
Or you can do it like
$ts=mktime(0,0,0,2,20,2015); // or
//$ts=strtotime('20/02/2015'); // inefficient
And your query can be modified to look like
WHERE insertTime=$ts
I would like to ask about how to filter by from default date until input date. query :
SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success
FROM transaction_201504
WHERE date BETWEEN '2015-03-23' AND LIKE '%" .$searchterm. "%';
I recommend to not use LIKE when using BETWEEN. You must put the exact value.
WHERE date BETWEEN '2015-03-23' AND '$searchterm';
where $searchterm has a format of yyyy-mm-dd (e.g. "2015-03-25")
since your date is in the format YY-M-D (and not D-M-YY / M-D-YY ) you can use string comparison like:
SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success
FROM transaction_201504
WHERE date >='2015-03-23' AND date <= $searchterm;
Hope it help a bit.
My query below doesn't seem to work.
SELECT * FROM `test` WHERE date between '12/30/2013' and '01/05/2014'
But when I change the order of dates like in this query below, it seems to be working
SELECT * FROM `test` WHERE date between '01/01/2014' and '01/05/2014'
What is the correct way to use date ranges in SELECTs?
It won't work because the dates are not ISO 8601 formatted: "2013-12-30" for example.
The BETWEEN clause makes a string comparaison, so you need either to use a correct date format in your database, or format the dates with DATE_FORMAT() or STR_TO_DATE(str,format).
Edit:
Try this query, which will work if you store dates as strings formatted as %m/%d/%Y, which is a bad idead, MySQL has a built-in DATE format :
SELECT * FROM test where STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE('01/01/2014', '%m/%d/%Y') and STR_TO_DATE('01/05/2014','%m/%d/%Y');
The DATE_FORMAT MySQL takes a DATE or DATETIME value as first argument, which you don't use, that's why it didn't work (in addition to the '$' you used instead of '%' before 'Y')
Use php and sql together to get the result as below
$date1 = date("Y-m-d", strtotime('12/30/2013'));
$date2 = date("Y-m-d", strtotime('01/05/2014'));
$sql = "SELECT * FROM `test` WHERE date between '".$date1."'
and '".$date2."'";
select * from test where date between "2013-12-30" and "2014-01-05"
This will work fine
I'm trying to write a conditional that would check a date in a table. If the date in the table is the same as todays date, it would ignore. If not it wouldn't break.
if(strtotime($row['timefield']) == (date(Y-m-d)){
break 1;
}
^ This doesn't work but it's sort of what I am trying to do. Thoughts?
You can just do it on the database side if your database is using the local time/date.
(Assuming MySQL, and timefield is a date type):
SELECT
CASE
WHEN timefield = CURDATE()
THEN 1
ELSE 0
AS is_today
If timefield is datetime then:
SELECT
CASE
WHEN DATE(timefield) = CURDATE()
THEN 1
ELSE 0
AS is_today
Assuming the timefield is of DATE type:
if (strtotime($row['timefield']) != date('Y-m-d')){
// Do insert
}
I'm pretty sure the problem is in the formatting of your datetime. Instead compere something that is much for comparable and rigid, like unix timestamp.
So basically what NullPointer just said... Damn that was quick :D