Solved

Convert Mysql varchar (10) dd/mm/yyyy to varchar (10)  YYYY-MM-DD format

Posted on 2012-04-10
2
365 Views
Last Modified: 2012-04-11
Our database contains a table called accountinfo with a column called fields_4 the current format is this:
3/15/2012      
3/16/2012      
3/17/2012      
3/18/2012      

This is what we would like to have.
2012-03-15      
2012-03-16      
2012-03-17      
2012-03-18      
We need to keep the format in varchar for scripting reasons not date format.

I was thinking something like this
Set Date_Field = Convert(datetime, VarChar_Field, 101) .
Thanks
0
Comment
Question by:CityInfoSys
2 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
Comment Utility
set Date_Field = str_to_date(VarChar_Field,'%m/%d/%Y')

While the output for str_to_date is of date datatype, it can be stored to a varchar field.  See below:

mysql> create table test_date(v varchar(10), d varchar(10));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into test_date (v) values ('3/15/2012'),('3/16/2012'), ('3/17/2012'), ('3/18/2012');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> update test_date set d=str_to_date(v,'%m/%d/%Y');
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from test_date;
+-----------+------------+
| v         | d          |
+-----------+------------+
| 3/15/2012 | 2012-03-15 |
| 3/16/2012 | 2012-03-16 |
| 3/17/2012 | 2012-03-17 |
| 3/18/2012 | 2012-03-18 |
+-----------+------------+
4 rows in set (0.00 sec)

Open in new window

0
 
LVL 1

Author Closing Comment

by:CityInfoSys
Comment Utility
Thanks for taking the time to show the example.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now