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.
Related
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;
In my database the column type is varchar(30) which stores date (24/02/2018), having multiple records.
i want the maximum date e.g i have 10/01/2016, 20/03/2017, 24/02/2018.
I am using the below query:
SELECT MAX(receipt_date) as rd FROM tblname
which returns me 10/01/2016 which is wrong.
i also tried to convert it to date format. but failed. mysql gives me syntax error.
SELECT CONVERT(varchar, mycolumn, 105) FROM tblname
but failed.
This is what I'd do:
ALTER TABLE tblname ADD COLUMN receipt_date2 DATE;
UPDATE tblname SET receipt_date2 = STR_TO_DATE(receipt_date, '%d/%m/%Y');
ALTER TABLE tblname DROP COLUMN receipt_date,
CHANGE receipt_date2 receipt_date DATE;
SELECT MAX(receipt_date) AS rd FROM tblname;
You can't use dates effectively in MySQL if you store than as VARCHAR in dd-mm-yyyy format. You should use a DATE data type. MySQL's DATE data type stores dates in yyyy-mm-dd format only. This way it can search for MAX() easily, it can sort them, it can do date arithmetic.
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
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.
In my table I've data in date field as 5/1/2012, it is of type varchar
I need to alter the column as date and update the date as 2012-05-01
Any idea?
Inorder to modify the table column_type perform:
ALTER TABLE table_name MODIFY COLUMN column_name date
But note that Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type
So if 5/1/2012 is the input value already in the table, it will be converted to 0000-00-00
Please refer http://dev.mysql.com/doc/refman/5.1/en/datetime.html for details
STR_TO_DATE(datestring, '%Y-%d-%m')
try this in your query....