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;
Related
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;
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.
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 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.
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