Mysql update a datetime field from an existing varchar field - mysql

I had to rebuild a table from a text file, and the data import created my datetime field as a string YY/MM/DD hh:mm:ss .. I have tried str_to_date and date functions, but nothing..
Update mydb set new-date-field = str_to_date( old-str-field, format );
Where format was various yy/mm/dd etc formats..
Thanks

If you truely have a text field of the format yy/mm/dd hh:ii:ss then you can do this to convert that string to a DateTime
UPDATE mytable set new_date_field = TIMESTAMP(old_str_field);
To test it first do this
SELECT TIMESTAMP(old-str-field) as new_date_field FROM mytable;
https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_timestamp

Related

How to store a time_t field to MySQL datetime?

I have extracted the modification time of a file using the struct stat structure:
long modtime = image_stats.st_mtime;
This returns 1508240128.
Now, I wish to store this value into a MySQL table which has datatype as datetime.
If I store it directly, it fails saying it is not a datetime format.
How do I store it?
You can use FROM_UNIXTIME to convert a timestamp into a DATETIME
Query
SELECT FROM_UNIXTIME(1508240128);
Result
FROM_UNIXTIME(1508240128)
---------------------------
2017-10-17 13:35:28
as insert query
Query
INSERT INTO
[table]
(datetime_column)
VALUES
(FROM_UNIXTIME(1508240128))

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')

Convert varchar column to date in mysql at database level

I have one column date1 which is varchar type
I want this column to date type.
I tried changing field but all date is converted to 0000-00-00.
format is dd-mm-yyyy but in varchar.
How can I convert the same date format but with date format using sql queries or similar but at database level ?
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
More about STR_TO_DATE function.
Since your column name is date1, you can replace column with date1 in the above syntax, and the code shall be:
UPDATE `table`
SET `date1` = str_to_date( `date1`, '%d-%m-%Y' );
The other answers here are risky, because if they go wrong you'll lose your data. A safer way to do this is to create a new field on your database with a DATE (or DATETIME if you need time as well) format, then to run a query like
UPDATE `table` SET `my_new_date_field` = STR_TO_DATE( `my_old_data_field`, '%d/%m/%Y');
In this way, if the %d/%m/%Y bit is wrong, you won't lose your data.
Once you're happy, you can delete the old data field and rename the new one.
use STR_TO_DATE Function of MySQL
FIRST you will need to update the value in date format.
UPDATE `tbl` SET `date1` = STR_TO_DATE(`date1`, '%d-%m-%Y') WHERE 1=1
THEN Convert the field to date.
Most importantly remember to insert date as Y-m-d format, after then.

Change string into datetime then copy data to new table and compare

I have been researching this for a week and still no luck. I have a txt file that loads into 'temploadsi' table via php. The text file will always have old data that is already in the database so I only want the "new" data. My idea was to have a loading table 'temploadsi' and then compare the time stamps and import only "new" data into table 'tempdatasi'. The issue is that the time stamp column is formatted like 'MM/DD/YYYY HH:MM:SS' and that will not go into the mySQL datetime field. Therefore I have it set as a text field. I would like to convert the text into a datetime field like 'YYYY-MM-DD HH:MM:SS'. I have looked at STR_TO_DATE and DateTime functions but unable to update 'temploadsi'. Can you all help out?
The reason why you failing to update the field is because the time of your data is 24:59:59 which is not valid. The maximum time i think is 23:59:59, so in order to convert that to date, you need to use the following format
SELECT DATE_FORMAT(STR_TO_DATE(datetime,'%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
FROM table1
See SQLFiddle Live Demo
So if you wnt to update your field, you can use this query
UPDATE tableName
SET your_columnname = DATE_FORMAT(STR_TO_DATE(your_columnname, '%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
and this query assumes that your column you are updating has VARCHAR datatype.
Something like Str_to_date('12/10/2012 23:59:59','%m/%d/%y %h:%i:%s')
Don't get why you couldn't find this though
[Mysql Help]
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date