Convert string date name to datetime or string date - mysql

In one of my DB column i have a string like this
19-Oct-19
i want to change this string to a proper date format like this
19-10-2019 or 19-10-2019
how can i achieve this with sql

With STR_TO_DATE() you can convert String to Date and with DATE_FORMAT() format your Date value.
select DATE_FORMAT(STR_TO_DATE('19-Oct-19', '%Y-%M-%d'), '%d-%m-%Y');

Related

How to convert string 'mm-yyyy' column to timestamp in MYSQL?

I want to convert string column of format 'mm-yyyy' to timestamp.
Try this following logic. As you have only Month and Year value in your string, I have added the static date 01 to each date to generate the DATETIME value.
select
CAST(
CONCAT(right('06-2019',4) , '-', left('06-2019', 2),'-01') AS DATETIME
);

Incorrect datetime value: '12-Mar-19' for function str_to_date

I have column which has the data type as date. I need to insert date values which are in string format and they follow this structure 12-Mar-19. I tried to use this mysql function STR_TO_DATE('12-Mar-19', '%c/%e/%Y %r') but it gives me an error: Incorrect datetime value: '12-Mar-19' for function str_to_date.
How can I overcome this.
You need to use a format string that matches your date literal:
STR_TO_DATE('12-Mar-19', '%d-%M-%y')

SQL Varchar to milliseconds

I have a column in my MySql database which stores a date as a string "dd/mm/yyyy" is there a way for me to have this in an update query where I can check if the date in the table is overdue compared to now().
I've had a look at the CONVERT function but can't figure out how to use it for my case.
STR_TO_DATE() converts the str string into a date value based on the fmt format string. The STR_TO_DATE() function may return a DATE , TIME, or DATETIME value based on the input and format strings. If the input string is illegal, the STR_TO_DATE() function returns NULL.
SELECT STR_TO_DATE('21/5/2013','%d/%m/%Y');
With time :-
SELECT STR_TO_DATE('01/01/2013 1130','%d/%m/%Y %h%i') ;
compare with now()
Where now()=STR_TO_DATE('01/01/2013 1130','%d/%m/%Y %h%i') ;
Try this:
SELECT ... FROM ... WHERE STR_TO_DATE(col, '%d/%m/%Y') > NOW()

MySQL date conversion("30-Jan-2013" to "2013-01-28")

In my page (PHP), I have date format like this: "30-Jan-2013". I want to convert this format to "2013-01-28".
Is it possible in MySQL date format?
use STR_TO_DATE to convert string to date
SELECT DATE(STR_TO_DATE(colName, '%d-%b-%Y')) -- date datatype
or
SELECT DATE_FORMAT(STR_TO_DATE(colName, '%d-%b-%Y'),'%Y-m-%d') -- string type

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