MYSQL Date query syntax not properly formatted - mysql

Here is how my column looks like
deeday
"06/07/15"
"02/07/15"
"06/07/15"
"04/07/15"
"06/07/15"
The following query works well
SELECT * FROM Bango ORDER BY STR_TO_DATE( `deday` , '%y/%m/%d' )
What am I missing in the following query to make it work.
SELECT * FROM `Bango` WHERE STR_TO_DATE( `deday` , '%y/%m/%d' ) = DATE_FORMAT(NOW(),'%d/%m/%y')
Thanks

Your query can work as per below-
SELECT * FROM `Bango` WHERE STR_TO_DATE( `deday` , '%d/%m/%y' ) = curdate();
You can use as per below but it will kill the performance, so you can remove " from your field one time-
SELECT * FROM
`Bango`
WHERE STR_TO_DATE( replace(`deday`,'"','') , '%d/%m/%y' ) = curdate();

SQL LIKE Statement on a DateTime Type
If the column type is datetime the LIKE operator doesn't work without converting the value to a varchar string on the fly.

Related

MYSQL - Date stored in different format dd-mm-yy - Cannot retrieve values

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

Date formatiing not working , mysql

I want to compare event filed of my table with current date. I have dates in event column in m/d/Y format i.e "09/24/2015" .
I am using this query for fetching result which get the records of current date but its returning empty result. I have a record for current date. what is wrong in it ?
SELECT *
FROM all_tasks
WHERE DATE_FORMAT( CURDATE( ) , '%d/%m/%Y' ) = DATE_FORMAT( date( event ) , '%d/%m/%Y' )
Assuming that you store the event as some sort of string, you can just do simply something like:
select * from all_tasks where DATE_FORMAT(NOW(),'%m/%d/%Y') = all_tasks.event
Here you go a fiddle sample.

How to split a MySQL field into two and compare string between both fields?

I've a field in my MySQL table financial_year that contains values like below
01-04-2010-31-03-2011
01-04-2011-31-03-2012
01-04-2013-31-03-2014
and I have a date suppose 03-05-2011
and want to get the financial year in which this date lies.
I tried it by using
SELECT financial_year
FROM financial_years
WHERE '03-05-2011' IS BETWEEN SPLIT("-", financial_year)
but it did not work.
Use LEFT() and RIGHT() since the length on your values is fixed and use STR_TO_DATE() to convert your string to date. Here is the example:
SELECT financial_year
FROM financial_years
WHERE STR_TO_DATE('03-05-2011','%d-%m-%Y') >= DATE( LEFT(financial_year,10) )
AND STR_TO_DATE('03-05-2011','%d-%m-%Y') <= DATE( RIGHT(financial_year,10) );
If the data type of financial_year is VARCHAR() you should use STR_TO_DATE() too like on this one
STR_TO_DATE(LEFT(financial_year,10),'%d-%m-%Y')
and
STR_TO_DATE(RIGHT(financial_year,10),'%d-%m-%Y')
The following code did work
SELECT financial_year
FROM financial_years
WHERE STR_TO_DATE('03-01-2012','%d-%m-%Y') >= STR_TO_DATE( LEFT(financial_year,10),'%d-%m-%Y' )
AND STR_TO_DATE('03-01-2012','%d-%m-%Y') <= STR_TO_DATE( RIGHT(financial_year,10),'%d-%m-%Y' );

Mysql - Multiple incorrect date format stored

I am working with data where the developer prior to me has been lazy and not stored the date in a set format. Its in as varchar and not a datetime. Needless to say its left me needing to sort the data.
Its in two formats
1) {dd}/{mm}/{yyyy} {hh}:{mm}
2) {dd}.{mm}.{yyyy} {hh}:{mm}
I would like to ensure that it is always returned in the mysql dateformat. The query below will get the first one.
SELECT
str_to_date( a.rentalstart, '%d/%m/%Y %k:%i' ),
a.*
FROM
jupiter1.table a
ORDER by createtime DESC
How would I combine the two? I would also need it default to a normal mysql datetime if it matches.
{yyyy}-{mm}-{dd} {hh}:{mm}:{ss}
SELECT CASE WHEN a.rentalstart LIKE "%/%/% %:%"
THEN str_to_date( a.rentalstart, '%d/%m/%Y %k:%i' )
WHEN a.rentalstart LIKE "%.%.% %:%"
THEN str_to_date( a.rentalstart, '%d.%m.%Y %k:%i' )
ELSE CAST(a.rentalstart AS DATETIME)
END AS rentalstart_good,
a.*
FROM ...
You can simple do REPLACE. It will turn all records in the format {dd}.{mm}.{yyyy} {hh}:{mm} and convert it to DateTime data type.
SELECT STR_TO_DATE(REPLACE(a.rentalstart, '/', '.'), '%d.%m.%Y %k:%i') newDate,
a.*
FROM jupiter1.table a
ORDER BY newDate DESC
SELECT
str_to_date( replace(replace(a.rentalstart, '.', '-'), '/', '-'), if(a.rentalstart like '%-%-%-%', '%Y-%m-%d %k:%i', '%d-%m-%Y %k:%i' )),
a.*
FROM
jupiter1.table a
ORDER by createtime DESC
Does this solve your problem?
Edited to include your default condition also

mysql extract year from date format

I need a mysql query to extract the year from the following date format from a table in my database.
For eg :
subdateshow
----------------
01/17/2009
01/17/2009
01/17/2009
01/17/2009
01/17/2009
the following query didn't working
select YEAR ( subdateshow ) from table
The column type is varchar. Is there any way to solve this?
Since your subdateshow is a VARCHAR column instead of the proper DATE, TIMESTAMP or DATETIME column you have to convert the string to date before you can use YEAR on it:
SELECT YEAR(STR_TO_DATE(subdateshow, "%m/%d/%Y")) from table
See http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
SELECT EXTRACT(YEAR FROM subdateshow) FROM tbl_name;
You can try this:
SELECT EXTRACT(YEAR FROM field) FROM table WHERE id=1
This should work:
SELECT YEAR(STR_TO_DATE(subdateshow, '%m/%d/%Y')) FROM table;
eg:
SELECT YEAR(STR_TO_DATE('01/17/2009', '%m/%d/%Y')); /* shows "2009" */
try this code:
SELECT DATE_FORMAT(FROM_UNIXTIME(field), '%Y') FROM table
This should work if the date format is consistent:
select SUBSTRING_INDEX( subdateshow,"/",-1 ) from table
TRY:
SELECT EXTRACT(YEAR FROM (STR_TO_DATE(subdateshow, '%d/%m/%Y')));
try this code:
SELECT YEAR( str_to_date( subdateshow, '%m/%d/%Y' ) ) AS Mydate