Date conversion query in SQL using STR_TO_DATE - mysql

In my table, there is a date column i.e. ORDER DATE which is in dd-mm-yyyy format, and the datatype of this date column is text. Now, I want to convert the format from yyyy-mm-dd. But the date format is not changing at all. Trying to use STR_TO_DATE but the year in the dates are coming as 2020 only. Not sure how to fix it or if there is a better way to convert the date in MySQL.
For example:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%y') AS DATE;
Result coming as 2020-09-14
Or suppose
SELECT STR_TO_DATE('20-MAR-2018','%d-%b-%y') AS DATE;
Result coming as 2020-03-20
day and month are coming properly but the year is not coming. Kindly help how to get convert the date format correctly.

Try to change your command like this below and you will get the answer:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%Y') AS DATE;
Refer to the documentation for more info about the commands.

Related

How to extract date and hour only from MySQL?

I have this column in MySQL table:
Date
2022-07-13 07:01:20
I want to extract this column to get this (the date and hour only):
2022-07-13 07
Any help is appreciated thanks!
This date column is in the form of timestamp
Apparently timestamp is a string so i can use substring.
To select the date and hour from that format first you have to take the substring using SUBSTRING()
the convert to datetime format using STR_TO_DATE()
you can take the specific date using SELECT CONVERT(date, your_date_time);
then extract the hour to add it in there SELECT HOUR()
you can then convert back to a string or use it that way

MySQL - How to convert date to the month has leading zeros?

So I'm trying to insert dates into a table and the date is in this format:
8/3/2021
However I want to add a leading 0 before the month and day so the date shows 08/03/2021. Also I want to add it as a string concatenated with another string so test123-08/03/2021
If you really store date in that format then you may try this:
SELECT
DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Use STR_TO_DATE() function to change the date value to standard MySQL date format of YYYY-MM-DD then use DATE_FORMAT() function to display the date value as per your desired output. The second operation is adding CONCAT() function on the converted date with your selected string. I'm assuming that your date value is d/m/y, because as #Stu mentioned in the comment, since you're not storing as MySQL standard date format, that means 8/3/2021 can be either d/m/y or m/d/y. With a standard date format value, the query would be shorter:
SELECT
DATE_FORMAT(date_col,'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(date_col,'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Demo fiddle
You should be inserting your source dates into a proper date or datetime column. Then, to view your dates in the format you want, use the DATE_FORMAT() function with the appropriate format mask:
SELECT DATE_FORMAT(date_col, '%d/%m/%Y') AS date_out
FROM yourTable;

STR_TO_DATE returns null

I'm trying to convert a string to date with a format of yyyy/mm to yyyy
STR_TO_DATE(SUBSTRING(time,1,4), '%Y')
This returns null for every value as well as when I try to convert without getting rid of the mm:
STR_TO_DATE(REPLACE(time,'/',''), '%Y%m')
This method has worked for me before, I'm at a loss for what I'm missing.
Thanks in advance!
Edit for clarification:
Eventually I am going to insert the year into a column with data type year, so I need to convert a varchar to a date type so I can extract the year in order to insert the data into a new table
I am in the process of making sure it will work before populating the new column with a command like this:
INSERT INTO table (year)
SELECT STR_TO_DATE(SUBSTRING(time,1,4), '%Y')
FROM `origtable`
I'm trying to convert a string to date with a format of yyyy/mm to yyyy
Just use left():
select left(time, 4) as yyyy
There is no need to convert to a date or datetime. You want a simple string operation.
EDIT:
year is a really weird type. I would just use an integer. But if you are using it, you can insert a full date into the field:
select date(concat(time, '/01'))
The resulting date can be inserting to a year column.

Find the number of days between today and specific date?

I want to find the number of days between today and selected date.
I used one query
SELECT DATEDIFF(CURDATE(), TILLDATE) FROM interestpr_table WHERE GLID="150";
But it returns NULL as result. How to fix?
As your TILLDATE is in format YYYY-MM-DD, you need to change it in YYYYMMDD format so that DATEDIFF would accept it as valid format.
SQLFIDDLE
For converting your date to format which DATEDIFF accept, you can use DATE_FORMAT like this :
SELECT DATEDIFF(curdate(),DATE_FORMAT(TILLDATE,'%Y%m%d')) // '%Y%m%d'converts it into YYYYMMDD format
SQLFIDDLE for conversion

Selecting date range MySQL with date_format

I've got an issue selecting a date range with MySQL.
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') BETWEEN '01-01-2010' AND '02-01-2010'
mvtDate type is date like 2010-01-01 00:00:00.
When I run the query, the result works for the days and the months but it also show me other result from other years.
Like 01-01-2011 etc.
You should use STR_TO_DATE since you want to convert string back to date
SELECT MvtDate, date_format(MvtDate,'%d-%m-%Y')
FROM `immmvt`
WHERE MvtDate BETWEEN STR_TO_DATE('01-01-2010','%d-%m-%Y') AND
STR_TO_DATE('02-01-2010','%d-%m-%Y')
FYI: DATE_FORMAT() converts date to formatted string representation.
STR_TO_DATE() converts formatted string back to date
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') IN ('01-01-2010', '02-01-2010')