Mysql - Multiple incorrect date format stored - mysql

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

Related

MySQL - VARCHAR to DATE returns null

Using Local Storage Connection within Toad Data Point. It's set up to use MYSQL.
EXTRACT_DATE is varchar(20) in the format of: 08282020
Trying to change that format so it appears as 08/28/2020 in my results set. However, I kept getting {null} returned.
I've tried a variety of different methods (see the code below) but keep getting the same result.
SELECT
EXTRACT_DATE,
DATE_FORMAT(EXTRACT_DATE, '%m/%d/%Y') AS NEW_DT,
STR_TO_DATE(EXTRACT_DATE, '%m/%d/%Y') AS NEW_DT2,
CONVERT (EXTRACT_DATE, DATE) AS NEW_DT3
FROM USERTABLE
GROUP BY EXTRACT_DATE
STR_TO_DATE turns a string into a date. To format the result, apply DATE_FORMAT like so:
select DATE_FORMAT( STR_TO_DATE('08282020', '%m%d%Y'),'%m/%d/%Y') AS NEW_DT from dual
It think it is simpler and more efficient to use sring functions:
concat_ws(
'/',
substring(extract_date, 1, 2),
substring(extract_date, 3, 2),
substring(extract_date, 5, 4)
) as new_dt
Or:
concat_ws(
'/',
left(extract_date, 2),
substring(extract_date, 3, 2),
right(extract_date, 4)
) as new_dt
As for the query you wanted to write: you need to convert to a date first, then back to a string. So:
date_format(str_to_date(extract_date, '%m%d%Y'), '%m/%d/%Y')

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