Date formatiing not working , mysql - 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.

Related

MySQL getting time in specific offset

SELECT (from_time user_offset) as start FROM `availabilities`;
I am trying to add current logged in user's timezone offset to the time column values I am fetching.
The value of from_time will be like 02:30:00
and value of offset will like +02:00
Does anybody know what would be appropriate approach for the same.
UPDATE:
I tried the following way:
SELECT id, TIME_FORMAT( (
TIME_FORMAT( from_time, '%H:%i' ) + '05:30' ) , '%H:%i'
) AS
START
FROM `availabilities`;
I got 00:00, but the value should have been 02:00, as the value of from_time is 20:30
I even tried
SELECT
id,
CONVERT_TZ(
from_time,
'+00:00',
'+05:30'
) AS `start`
FROM availabilities
But it works only if **from_time field has both date and time, for time it returns null**
Converting timezones its not just adding +/-X hours.
It certainly should be more complex thing if you wants proper results.
I believe somthing like this may help
SELECT
id,
SUBSTRING(CONVERT_TZ(
CONCAT('2000-01-01 ', from_time),
time_format(TIMEDIFF(NOW(), UTC_TIMESTAMP), '+%H:%i'),
user_offset
), 12, 5) AS `start`
FROM availabilities

MYSQL Date query syntax not properly formatted

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.

How to Use CURDATE() in FPDF

I want to put the current date in FPDF Cell.
I used the following query but I don't know how to put it there.
SELECT DATE_FORMAT( CURDATE( ) , '%d/%m/%Y' )
If you just want the current date, you can use PHP's date function:
$currentDate = date("j/n/Y");
If you need to use SQL, you can use this query to achieve the same thing:
SELECT CONCAT(DAYOFMONTH(CURRENT_DATE), '/',
MONTH(CURRENT_DATE), '/',
YEAR(CURRENT_DATE));
Note that these do not include leading series. Date will be in the format of 5/6/2015.

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' );

Trouble with date format when comparing two dates in mysql

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';