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
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.
The dates in my database are stored as dd/mm/YYYY
How can I construct a simple mySQL query to pull dates within a certain range:
e.g.
SELECT * FROM metric WHERE date BETWEEN '31/01/2016' AND '01/02/2017'
You can use str_to_date but remember that now the server can't use index on the date column if any.
select *
from metric
where str_to_date(date,'%d/%m/%Y') between '2016-01-31'
and '2017-02-01'
It's better to store the date as date or if string then in standard format yyyy-mm-dd.
You should do the following:
Update all the columns that hold a date as a string to a date column
UPDATE metric SET `dateColumn` = str_to_date(`dateColumn`,'%d/%m/%Y');
Alter table
ALTER TABLE metric MODIFY `dateColumn` DATE;
Modify your code to insert valid dates to mysql
Use built in mysql date functions for your task
SELECT * FROM metric WHERE date BETWEEN '2016-01-31' AND '2017-02-01'
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.
I have a date field (tinytext) holding date information in format of "dd-mm-yy" e.g 07-01-90. Using a mysql query I want to change it to yyyy-mm-dd date format. I tried the code below but nothing happens.
mysql_query("UPDATE Table SET date=STR_TO_DATE('date','%Y,%m,%d')");
You're using the correct function STR_TO_DATE(str,format) to achieve the goal, but you're making two mistakes:
In your query the format argument does not match the string expression format. You said it's in dd-mm-yy format while you passed %Y,%m,%d (comma separated) to the format argument. The query will return a "incorrect datetime value" error. You should use %d-%m-%Y.
You can't change data type of a column on the fly, by setting different type of the value being passed. You have to first update the values and then change data type for column.
So, summarizing:
mysql_query("UPDATE `Table` SET `date` = STR_TO_DATE(`date`, '%d-%m-%Y')");
mysql_query("ALTER TABLE `Table` CHANGE COLUMN `date` `date` DATE");
Additionally, consider switching to the recommended PDO extension in place of old and slowly deprecated mysql extension.
Error in your query
is STR_TO_DATE(date, '%Y-%m-%d')
mysql_query("UPDATE Table SET date=STR_TO_DATE(date,'%Y-%m-%d')");
Try this:
INSERT INTO table(date_field) VALUES(STR_TO_DATE('December 8, 2010','%M %d,%Y'));
Try it with DATE_FORMAT() function.
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%Y,%m,%d')");
To display 2 digit year
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%y-%m-%d')");
To display 4 digit year
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%Y-%m-%d')");
I'd say you have to do this:
UPDATE table_name SET date = DATE_FORMAT('date', %Y-%m-%d);
If you are using my_sql with php, you can use date function