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';
Related
I have a table with the following values.
As you can see in the picture date is not stored in standard mysql date format.
It is stored in dd-mm-yy
I want to select the rows for which the invoicedate is before 20-09-2017, so it should contain row 13-09-17.
I tried the following query, but it doesn't anything.
SELECT *
FROM `invoices`
WHERE date_format(invoicedate,'%d-%m-%Y') < date_format(20-09-17,'%d-%m-%Y')
Use 2017 as the year in your date, and wrap the date in a apostrophes:
SELECT *
FROM `invoices`
WHERE date_format(invoicedate,'%d-%m-%Y') < date_format('20-09-2017','%d-%m-%Y');
In my side the result of date_format('20-09-2017','%d-%m-%Y') is null,this might the reason for you.
Try with below(it works fine in my side):
SELECT *
FROM `invoices`
WHERE invoicedate < STR_TO_DATE('20-09-2017', '%d-%m-%Y');
SELECT *
FROM `invoices`
WHERE strtotime(invoicedate) < strtotime(20-09-17)
SELECT *
FROM invoices
WHERE STR_TO_DATE( invoicedate, '%d-%m-%Y' ) < STR_TO_DATE( '20-09-2017', '%d-%m-%Y' )
LIMIT 0 , 30
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')
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.
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'