How to convert varchar2 to date in sql or mysql

2.39K views
0
0 Comments

I want to convert a string from varchar 2 to Date in DD-MM-YYYY format. I am storing the date as a string in the ’01-09-2021′ format. I want to pass a date field in SQL query as a date format to retrieve the data using between.

 

 

 

htmlAdmin2609 Changed status to publish January 29, 2021
Add a Comment
0

You can simply use the date_format() to convert from varchar2 to date.

Syntex

SELECT DATE_FORMAT(STR_TO_DATE(fieldName, '%D-%M-%Y'), '%Y%m') as DateFormat from tableName;

OR

select date_format(str_to_date('01/01/2021', '%d-%m-%Y'), '%Y%m');

MySQL CAST() Function: You can also convert a string value to date datatype

select cast(“02-08-2020” as date);

Try this

https://stackoverflow.com/questions/18597051/sql-date-format-convert-dd-mm-yy-to-yyyy-mm-dd

 

 

 

 

 

 

 

 

 

htmlAdmin2609 Changed status to publish January 29, 2021
Add a Comment
Write your answer.
close