Conver datetime format in MySQL - mysql

I have a column with value 9/12/2012 00:00:00.000000 in a MySQL table, how do I update the table to make the value like 2012-12-9 00:00:00.000000.

The best thing to do would be to convert your text date column to a proper datetime column using STR_TO_DATE:
UPDATE yourTable
SET newDate = STR_TO_DATE(oldDate, '%m/%d/%Y %H:%i:%s.%f');
Once you have done this, you may view your datetime column any way you want:
SELECT DATE_FORMAT(newDate, '%Y-%d-%m %H:%i:%s.%f') AS newDateFormatted
FROM yourTable;

Related

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.

why does mysql update erases field data?

mysql update deletes the field data on some rows rather than update them when i run this mysql statement
UPDATE
tablename
SET
date = DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d")
I am trying to update date formats from 05/08/2013 to 2013-05-08 to make it so I can search where date between $date and $date
try this:
UPDATE tablename SET `date` = STR_TO_DATE(`date`, '%d/%m/%Y')
and then change the datatype of the column like this
ALTER TABLE tablename CHANGE COLUMN `date` `date` DATE
OR ALternatively
update tablename SET date = DATE_FORMAT(STR_TO_DATE(date,'%d/%m/%y'),'%y-%m-%d')
You should make sure that your date field is true DATE or DATETIME datatype. After that,
UPDATE tablename
SET `date` = STR_TO_DATE(old_date, '%d/%m/%Y')
Then, you would be able to do all normal math on dates: add, subtract, compare, sort, etc.
MySQL STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments.
If the date or time or datetime value specified as string is illegal, the function returns NULL so check all value in select statement first to know if its returning null
Select DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d") from tablename

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.

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

Date format :from varchar to integer

i am having the date in the format like 'jan 2011' but if i use between 'jan 2011' and 'mar 2011' it is giving the wrong months
so i want to convert the varchar into the number format so that i can use between query in mysql
thanks in advance
you should convert the varchar column to date column, which is more foolproof
(varchar lead to error prone)
use find_in_set to do the mapping
select find_in_set('jan', 'jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec');
so
select
find_in_set(
substring_index(col, ' ', 1),
'jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec'
)...
Add a real date column to your table:
ALTER TABLE the_table
ADD COLUMN the_real_date_column DATE;
Use STR_TO_DATE to insert a date value into that column:
UPDATE the_table
SET the_real_date_column = STR_TO_DATE(the_bad_date_column, '%b %Y');
STR_TO_DATE will insert a 0 for each value not supplied—in this case the day portion of the date. So you can increment the day to 1 like this:
UPDATE the_table
SET the_real_date_column = the_real_date_column + 1;
Then you can query the date column normally, use BETWEEN, all that good stuff.
If seeing the old column bothers you, you can drop it like this:
ALTER TABLE the_table
DROP COLUMN the_bad_date_column;