Solved

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

Posted on 2012-04-10
2
401 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
[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 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37830814
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
ID: 37832846
Thanks for taking the time to show the example.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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