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.
Related
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'
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 column in a table in MySql, which contains dates as string in any format (there is not fixed pattern) it could be as m-d-y or M-D-Y or m/d/y or YY/MM/DD or MM/DD/YY or mm/dd/yy etc. My question is how should I detect it and then change to some particular format i.e mm/dd/yy.
If you can guess the number of different formats you stored the dates in a varchar field then it would be easier to deal with the problem;
One way would be put all those different formats in the query given below:
SELECT
COALESCE(
STR_TO_DATE(your_date_column,'%m-%d-%Y'),
STR_TO_DATE(your_date_column,'%M-%D-%Y'),
STR_TO_DATE(your_date_column,'%m/%d/%Y'),
STR_TO_DATE(your_date_column,'%m-%d-%Y'),
....
.
.
)
FROM your_table;
Demonstration:
SET #your_date_field := '8/8/2016';
SELECT
COALESCE(
STR_TO_DATE(#your_date_field,'%m-%d-%Y'),
STR_TO_DATE(#your_date_field,'%M-%D-%Y'),
STR_TO_DATE(#your_date_field,'%m/%d/%Y'),
STR_TO_DATE(#your_date_field,'%M/%D/%Y')
);
Output: 2016-08-08 (yyyy-mm-dd)
Note:
But dates should be stored in a date datatype. Violating this will put towards this kind of cumbersome situation.
So, better move these date strings to date datatype column.
More: In order to move these date strings to a date datatype column you can follow the steps below:
ALTER TABLE your_table ADD COLUMN date_new date;
UPDATE
your_table
SET date_new = COALESCE(
STR_TO_DATE(your_date_column,'%m-%d-%Y'),
STR_TO_DATE(your_date_column,'%M-%D-%Y'),
STR_TO_DATE(your_date_column,'%m/%d/%Y'),
STR_TO_DATE(your_date_column,'%m-%d-%Y'),
....
.
.
);
ALTER TABLE your_table DROP COLUMN `date`;
ALTER TABLE datestable CHANGE COLUMN `date_new` `date` date;
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 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.