Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
edited by

I am having a string column which acts as a date and I need to select it as a date.

Will it be possible?

My sample data would be in this format, month/day/year -> 12/31/2011

1 Answer

0 votes
by (12.7k points)
edited by

You can try the following query:

SELECT  STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM    yourtable

You can likewise manage these date strings in WHERE clauses. For example,

SELECT whatever
  FROM yourtable
 WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAY

You can manage all kinds of date/time layouts this way. Kindly refer to the format specifiers for the DATE_FORMAT() function to view what you can use into the second parameter of STR_TO_DATE().

If you are a beginner and want to learn SQL, then do check out the SQL Tutorial that will help you out in a better way. 

Related questions

0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Feb 18, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 15, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories

...