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
Related
I have the following code, with the column SaleDate in a datetime format, and I am trying to update it in the table to a date datatype instead
UPDATE nashhousing
SET SaleDate = CONVERT(SaleDate, DATE)
and
UPDATE nashhousing
SET SaleDate = CAST(SaleDate AS DATE)
I've tried both cast and convert, but neither modifies the table, does anyone know what's wrong?
You need to ALTER the table structure for that:
ALTER TABLE nashhousing Modify column SaleDate date
This will try to convert the date strings into real dates. Check the data afterwards if it succeeded. If not, you need to change the strings first to the right format. The default date format that always works is YYYY-MM-DD.
Of course if this is live data you should test that with a backup table first.
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;
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.
CSV date format is DD/MM/YYYY like this 16/11/2016. All the date become 0000-00-00 in MySQL. How to solve this differences?
As of now I can think of two solutions to you problem:
Create a additional VARCHAR field to insert those values (like 16/11/2016), and create a TRIGGER on INSERT to update the date field by converting the string date to 'YYYY-mm-dd' type.
These links may help
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
http://www.w3resource.com/mysql/date-and-time-functions/mysql-str_to_date-function.php
In this step also, Create a additional VARCHAR field to insert those values (like 16/11/2016) and after import is done run a UPDATE query to update your date field using SET dateField = convertDate(dateFromSheet)
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.