Solved

MySQL update text field to date field

Posted on 2014-01-02
5
584 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 250 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 250 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

Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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