?
Solved

MySQL update text field to date field

Posted on 2014-01-02
5
Medium Priority
?
585 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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

762 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