I have a field in a table called created_at which i would to compare with two different dates.
e.g. created_at BETWEEN '2011-08-01 13:23:21' AND '2011-08-07 13:23:21'
i like to show all records which created in these two dates.
i have tried different method but still with no luck.
First be sure to set your date_field to DATETIME type, then do the query like this:
SELECT * FROM your_table WHERE date_field >= '2011-08-01 13:23:21' AND date_field <= '2011-08-07 13:23:21'
SUBSTRING(now(), 1, 10) = '2015-0-15'
Related
I can't find why DATE(created) LIKE '2016-1%' does not return from database anything, while DATE(created) LIKE '2016%' will return the row with date 2016-1-22, this column is timestamp format with CURRENT_TIMESTAMP.
SELECT * FROM logs WHERE DATE(created) LIKE '2016-1%'
I can't find the row either with 2016-1-22 ...
The DATE part of a TIMESTAMP column is retrieved by MySQL in the format 'YYYY-MM-DD' (see documentation). So a date like 2016-1-22 is actually represented as 2016-01-22, which does not match '2016-1%'.
Try prefixing month filed with 0, till 9(i.e. 01,02,03,..).
So your query should become like
SELECT * FROM logs WHERE DATE(created) LIKE '2016-01%'
I'm trying to select rows which contains a specific number after current date.
My code looks like this:
SELECT *
FROM MyTableName
WHERE ( _001 ) AND My Date Column > todays date
LIKE '%2%'
I'm looking for the number 2 in _001.
"My Date Column" is describing the date linked the row.
you can use SUBSTR function for it, like this -
SELECT * FROM MyTableName WHERE SUBSTR(your-date-col,0,1) = '2' AND your-date-col > CURDATE()
As you describe it, the code would look like:
SELECT *
FROM MyTableName
WHERE `_001` = 2 AND datecol > CURDATE();
Note that now() contains a time component, which can throw off logic (although it would probably work in this context).
use MySQL CURDATE()
Date > CURDATE() //return current date
or try this query :-
SELECT *
FROM MyTableName
WHERE ( _001 ) AND date > CURDATE()
LIKE '%2%'
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';
im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.
my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453
If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')