Copy values one MySQL column to another, but formatted - mysql

I have added a DATE column to a table, but now need to populate that DATE column with the values from another column - except that original column is an INT. The INT column is mmddyyyy. Is there a way to copy and format using
UPDATE `table` SET int_column = date_column

Try this using str_to_date and lpad functions:
UPDATE `table` SET date_column = str_to_date(lpad(int_column, 8, 0),'%m%d%Y')
Why used lpad(int_column, 8, 0) - When date is, say, 02012017, the direct cast to char will convert it into 2012017, for which str_to_date function will return null. Lpad pads required 0 to make length 8 and hence outputs 02012017 which str_to_date function will correctly convert.

Related

MySQL: How to set a DATE column by converting all values in a VARCHAR column?

I have a table in which there is a column called "DATE" which contains dates in the format "23-Nov-2017" as datatype VARCHAR. I'm trying to convert this VARCHAR column and store it in a new column called "NEWDATE" of datatype DATE.
I have created the new column "NEWDATE" of type DATE and I am trying to use the STR_TO_DATE() function to perform the conversion. However, I can't get it to work for some reason.
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%m-%Y');
The NEWDATE column is not updated with any values after the statement. I guess this means that the statement does not execute. What am I doing wrong?
EDIT: I have also tried STR_TO_DATE(DATE,'%d-%b-%Y'). However there is still no change to the values in the NEWDATE column
Your format '%d-%m-%Y' does not match your actual date string "23-Nov-2017"
The %m is for numeric month and you have an abbreviated text month
Use %b for 3 char month values like this:
STR_TO_DATE(DATE,'%d-%b-%Y')
EDIT: WorkBench issue
That is just a Workbench config setting to stop you accidentally issuing a HUGE update. You can either turn that setting OFF or frig it a bit by giving it a WHERE clause that will allow it to run like below. Below assumes this table has an id column
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id<10000000;
Or
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id>0;

Cannot get row data after SELECT

I'm a bit new to PHP as well as MySQL and I'm having an issue (I'm not even sure if this is possible) here's my issue:
I have this table here:
And I use this statement:
SELECT *
FROM (SELECT DATE_FORMAT(STR_TO_DATE(SPLIT_STRING(`date`,' ',1), '%m/%d/%Y'), '%Y-%m-%d') as month
FROM `automation-reports`
) as innerTable
WHERE MONTH(month) = 5
Which gives me:
And I want to be able to get success from that row that contains it here:
I'm not sure if this is even possible considering now data is returned for that row but like I said I'm new to MySQL so I'm not sure of the limitations.
Given that your date column is not a real DATETIME type, you will need to use STR_TO_DATE() but you can use a more complete date string format to return a full DATETIME object from it all at once. The correct format string is '%m/%d/%Y %r', where %r is the 12 hour time hh:mm:ss followed by AM/PM. Using that format, you can wrap the entire output in MONTH() either in SELECT or in WHERE.
SELECT
ID,
reportid,
report,
success,
STR_TO_DATE(date, '%m/%d/%Y %r') AS realdate
FROM
`automation-reports`
-- Apply MONTH() in the HAVING
HAVING MONTH(realdate) = 5
Alternatively instead of HAVING you can put the whole expression in WHERE
...
WHERE MONTH(STR_TO_DATE(date, '%m/%d/%Y %r') = 5)
But really, I would recommend changing that column to a proper DATETIME, as doing so will open up all of MySQL's date processing functions for you and allow the RDBMS to index and optimize the column. You cannot really change the column in place and have MySQL correctly parse the dates. Instead you need to add a new column, fill it, then remove the old column and rename the new (unless you want to keep both).
-- Add a DATETIME
ALTER TABLE `automation-reports` ADD realdate DATETIME;
-- And fill it with dates parsed from your string column
UPDATE `automation-reports` SET realdate = STR_TO_DATE(date, '%m/%d/%Y %r');
-- Drop the old column if you do not need both
ALTER TABLE `automation-reports` DROP date;
-- And rename the new one to the old name
ALTER TABLE `automation-reports` CHANGE realdate date DATETIME;
In this case, you can set the display format of the date after you query it, which is the better course of action than storing the date as a string in the format you want to query it.
If you are in any position to rename this table right now, I would also recommend changing the table name from automation-reports to automation_reports because MySQL does not require the backtick-quoting for the underscore name, while you'll always be needing backticks with the hyphenated name.

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.

mysql syntax for populating a column of an existing table and setting a default value for said column

I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?