Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL update text field to date field

Posted on 2014-01-02
5
Medium Priority
?
592 Views
Last Modified: 2014-01-02
I have a database where the date field is text field and need to fix that.  What is the best way to do that.

If date1 is a text field with 01/01/2014
and date 2 is a date field and needs to display 2014-01-01.

Would you use substr to get the numbers out, something like this conceptually, if so, I have no idea how to put them all together?

update table set date2 = date_format('substr(`date1`, 0, 2)',  '%Y-%m-%d')


Thanks in advance, hope everyone had a great new year!
0
Comment
Question by:ststesting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Author Comment

by:ststesting
ID: 39751630
Just found this.
update table set date2 = STR_TO_DATE(`date1`, '%Y-%m-%d')

and get
Error 1411. Incorrect datetime value 'date1' for function str_to_date
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39751734
Remember the format you enter is one that matches the data in `date`.  Date data type does not store in a specific format; therefore, you are not looking to format it as 2014-01-01, you simply want to convert to a date.
0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 1000 total points
ID: 39751744
can you just try:

STR_TO_DATE(date1, '%Y-/%m-/%d') .

date1 without the quotes?
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 total points
ID: 39751750
I doubt it is the quoted identifiers.  Here is an example of what I mean.
SELECT STR_TO_DATE(date1, '%m/%d/%Y') date2
FROM (
    SELECT CONVERT('01/01/2014', CHAR(10)) date1 
    UNION SELECT '12/30/2013'
) tbl
ORDER BY date2
;

Open in new window

0
 
LVL 2

Author Comment

by:ststesting
ID: 39751901
Thanks guys!

I was using str_to_date like date_format and what putting what I wanted the output to be rather that telling it want it wanted (%Y-/%m-/%d vs %m/%d/%Y).  (dumb moment)

update tbl set date2 = STR_TO_DATE(`date1`, '%m/%d/%Y')

Also this worked.
update tbl set date2 = concat(substr(date1, 7, 4),'-', substr(date1, 4, 2),'-', substr(date1, 4, 2))
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question