Add column to table that has converted varchar to date format - mysql

I have a table with a variable, DATE, in varchar format that looks like this:
DATE
2009-09-23 00:00:00
2004-11-14 00:00:00
etc...
I am trying to add a column named DATE_X that contains the DATE string converted to datetime format. Can this be accomplished with STR_TO_DATE()? How do I use this without changing the original column DATE, but rather adding a new column DATE_X?
Thanks!

alter table TableName add column date_x datetime;
update table
set date_x = date;
Assigning a string to a datetime column will parse it.

Related

Alter Query To Change Date Format

How can i use the alter query to change a DATE format in mysql
for example
ALTER table userdata ADD column DateofBirth DATE SET = '%d-%m-%Y';
I tried this and it didnt work.
If your dates are stored in column with type DATE. You can set date format in SELECT query:
SELECT DATE_FORMAT(DateofBirth ,'%d-%m-%Y') AS DateofBirth FROM userdata;
There is no "date format" specified with the DATE datatype. It's not possible to specify a format with the column definition.
MySQL does provide a couple of useful functions... STR_TO_DATE and DATE_FORMAT that convert between DATE and string representations, in a variety of formats.
Reference: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
If am not wrong, you need a update statement
Update userdata set DateofBirth = DATE_FORMAT(DateofBirth ,'%d-%m-%Y')
But it is better to store dates in DATE datatype

Converting MM-DD-YYYY varchar to datetime

I have 5000+ dates in the following format.
00-00-0000
And the column data type is varchar. If I change the column type then all my rows are put to 00-00-0000 rather than converting the string literal to a date.
Is it possible to change all 50000+ rows to datetime and also the column data type? What would be the best way to do this?
Create a temporary DATE column and update it using STR_TO_DATE function:
UPDATE mytable
SET temp_date = STR_TO_DATE(varchar_date, '%m-%d-%Y')
Then drop the varchar date column and rename the temp date column.

Converting String Data Value into date

Good Morning All;
I currently have a MySQL table where there are 3 date fields (Columns) that were loaded as strings in this format 20140101 YYYYmmdd. I would like to convert this to a date format 2014/01/01 YYYY/mm/dd. Can someone please provide a simple sql syntax that would alter the table to a date format from a string and change the column to display the dates like this 2014/01/01 and not like 20140101. Thanks to all
Try this:
date_format(str_to_date(datecolumn, '%Y%m%d'),'%Y/%m/%d')
If you just want to reformat the values in the VARCHAR column, assuming that the column with sufficient length e.g. VARCHAR(10), and all the values are eight characters in length...
You could do something like this:
UPDATE mytable t
SET t.mycol = CONCAT( LEFT( t.mycol ,4)
, '/'
, SUBSTR( t.mycol ,5,2)
,'/'
, SUBSTR( t.mycol ,7,2)
)
WHERE CHAR_LENGTH(t.mycol) = 8
We want something in the statement that will prevent the statement from "working" a second time, if it's inadvertently re-run. It doesn't have to be CHAR_LENGTH. We might want to include a check that the value doesn't already contain a slash character AND t.mycol NOT LIKE '%/%'.
But why on earth are "date" values being stored in character columns, rather than in DATE datatype, which is custom designed for storing and working with date values?
ALTER TABLE mytable MODIFY mycol DATE ... ;
(If the column is defined as NOT NULL, has a default value, has a comment, those attributes can be retained, they need to be included in the new column specification, e.g.
ALTER TABLE mytable MODIFY mycol DATE NOT NULL COMMENT 'creation date';
Note that DATE columns do not have a "format" per se. When converting to string, MySQL uses date format '%Y-%m-%d'. And MySQL expects string literals representing date values to be in that same format. To get a value from a DATE column converted to string in format 'yyyy/mm/dd'.
SELECT DATE_FORMAT(date_col,'%Y/%m/%d') AS date_col
To get a string value in that format converted to DATE datatype
SELECT STR_TO_DATE('2015/06/01','%Y/%m/%d')

Format date for mysql insert

Im using LOAD DATA INFILE to import a csv file, the files date format is 29/11/2010 and the database format is 2010-11-29, what can i use to format the date inside the query?
I've tried str_to_date:
SET date_start = STR_TO_DATE(#from_date,'%Y-%m-%d'),
but that only inserts 0000-00-00
MySQL 4.x
LOAD DATA will try to insert your dates as they are. It isn't aware about format and in common case you can not apply some post-processing to your fields (well, different from some format which is allowed inside LOAD DATA syntax itself) - and you can not adjust your values via SET keyword like in MySQL 5.x
Instead you can do following steps:
Declare your table's column as VARCHAR. Let it name be record_date
Do your LOAD DATA query. It will load your dates into record_date column
Add new column to your table, let it be temp_date - with type DATE: ALTER TABLE t ADD temp_date DATE
Update your temp_date column: UPDATE t SET temp_date = STR_TO_DATE(record_date, '%d/%m/%Y')
Drop your VARCHAR date column: ALTER TABLE t DROP record_date
Finally, rename column with correct DATE type to original one: ALTER TABLE t CHANGE temp_date record_date DATE
As result, you'll have your dates loaded into your table as DATE date type. Replace record_date to the name which your original column has.
MySQL 5.x
You can use SET keyword and natively replace procedure, described above. So just do:
LOAD DATA INFILE 'file.csv'
INTO TABLE t
(#date)
SET record_date=STR_TO_DATE(#date, '%d/%m/%Y')
-sample above is for one column and you'll need to add others (if they exist). Date column name is also record_date - so change it to actual name too.
Try something like
update tablename SET date_start = date_format(#from_date,'%Y-%m-%d')
Use DATE_FORMAT() . It will Formats the date value according to the format string. Mysql
date_format(#from_date,'%Y-%m-%d')

Changing the column Type in SQL

Hey I have an SQL Table which has a column for storing date but the date column has a type varchar. I want to change the type to date but I don't want the actual data to be lost in that column. How can I achieve that.
Manually taking a backup of the table and then entering each entry? or there is some other cool way to do it ? Actually the data is huge
Thanks
My way of doing this:
(1) Add a new column:
ALTER TABLE yourtable
ADD COLUMN `new_date` DATE NULL AFTER `views`;
(2) Update the new column
UPDATE yourtable SET new_date = old_date;
Take care of the datas formatting in old_date. If it isn't formatted yyyy-mm-dd, you might have to STR_TO_DATE or some string-replacements in this UPDATE-statement here to fit your purposes.
Example:
If your data looks like this: mmmm dd, yyyy, hh:mm (p.e. May 17, 2012, 8:36 pm) , you can update like this:
UPDATE yourtable
SET new_date = STR_TO_DATE(old_date, "%M %e, %Y");
STR_TO_DATE basically reverse engineers string data to a date value.
(3) Delete the old column
ALTER TABLE yourtable
DROP COLUMN `old_date`;
(4) Rename the new column
ALTER TABLE yourtable
CHANGE `new_date` `old_date` DATE NULL;
Done!
What about:
1) Adding a new column with the right type
2) Updating your new column with the parsed dates
3) Removing the old column
Update to fill the date pattern parsing requirement:
SELECT STR_TO_DATE('May 17, 2012, 8:36 pm','%M %d, %Y');
Add a new column with data type you want, then run an UPDATE query to copy the data from the old column to the new column. and then delete the the old column.
Note that perhaps you will have to use the CONVERT function to convert the date string into a datetime.
Use this query:
ALTER TABLE tablename MODIFY COLUMNNAME Datatype
e.g.
ALTER TABLE Users MODIFY RegisterDate DateTime