I have some office records. Database is already created and filled with lots of data. I have to create search functionality on it. But the problem is database have date field which is in var char form and is in the form "DD-MM-YYYY" eg :"18-11-2011"
I want to convert the date column in date format without losing this data. I tried doing it on test table and everything turned to zero. eg: 0000-00-00
What can be done for the same ?
To expand on flesk's answer add a new column
ALTER TABLE foo ADD newdate DATE;
Update the table to fill this new column (like flesk did)
UPDATE foo SET newdate=str_to_date(olddate, '%d-%m-%Y');
Then you can even test if the conversion is correct.
SELECT * FROM foo WHERE olddate <> DATE_FORMAT(newdate, '%d-%m-%Y');
Then you can either drop old column and rename new one. Or just leave it as it is.
Related
I have one field in a table that has a time as a value.
table name: sessions
column: time
For example
time
-----
09:00:00 (this should be the correct time)
12:00:00:00:00
09:00:00:00
11:00:00:00:00:00:00
16:00:00:00:00
This table got messed up and I would like to clean it and keep only the 8 first characters of each row and delete everything that is after that.
Is there a way i can do this with a mysql command?
Thanks
All you need is to update the table:
update sessions
set time = left(time, 8)
Use string functions:
update mytable set mytime = substring(mytime, 1, 8)
I would also recommend changing the datatype of your column to time, so the database will properly enforce data integrity for you in the future. If your data can be implicitely converted to the target format (which should be the case once after executing the above query), you can just do:
alter table mytable modify mytime time;
Note that time is not a wise choice for a column name, since it conflicts with a MySQL datatype. I used mytime instead in the queries.
I currently have a Release_Date(Date) in my Songs table. I been trying to change the date format. The current format is yyyy-mm-dd. I want mm/dd/yyyy.
Error: Invalid Date value.
Release_Date is stored in the database as a Date, not as a string, so you don't need to call the str_to_date function. You are getting an error because you are calling the str_to_date function on something that is already a date, not a string.
Furthermore, as it is a date, you can't update that field to a string value. You would have to create a new column defined as a string and store the date there.
However, it is highly advantageous to keep the dates stored as Date fields, because comparisons, sorting, and the various date functions will all work as they should.
So if you want to use the date in a different format, you would just use DATE_FORMAT(Release_Date,'%m/%d/%Y') whenever you access it, and leave the field as a native date, as in
SELECT DATE_FORMAT(Release_Date,'%m/%d/%Y') FROM Songs WHERE Release_DATE IS NOT NULL;
It is not possible to "update" the internal format of a MySQL date. If you want to display your text as mm/dd/yyyy, then you should only need a single call to DATE_FORMAT, e.g.
SELECT DATE_FORMAT('2019-07-29', '%m/%d/%y')
which prints:
07/29/19
As Tim suggests, you don't need to change the existing date format & value but if you insists, you could add another column - datatype VARCHAR - in the table and then update the column according to your desired date format. Steps are below:
create new column
ALTER TABLE songs
ADD COLUMN `rls_date` VARCHAR(50) AFTER `release_date`;
Update new column with desired date format
UPDATE songs SET `rls_date`=DATE_FORMAT(`Release_Date`,'%m/%d/%Y');
Just remember, by doing this, you can't expect the column to identify any date format related function outright. Lets say you run a query like this SELECT * FROM songs WHERE rls_date=CURDATE(); won't work.
I am having`a whole mysql table of approx 40,000 rows with a column named 'epoch_time' and there is epoch time in it and I want to convert that whole table's 'epoch_time' to a equivalent 'date' together in a single sql query and I'm doing this is in php_my_admin . Thanks in advance.
I guess by epochtime you mean UNIX-style timestamps, that is, number of seconds since 1970-01-01T00:00Z. If my guess is wrong, so is the rest of my answer.
First you add a new column to the table.
ALTER TABLE mytable ADD COLUMN datestamp DATETIME AFTER epochtime;
This names the new column datestamp and puts it right after epochtime in the list of columns.
Then you update the whole table to populate the new column from the old using FROM_UNIXTIME(). Omitting the WHERE clause makes the update work on the whole table (careful!).
UPDATE mytable SET datestamp = FROM_UNIXTIME(epochtime);
Finally, if you wish you can drop the old column.
UPDATE TABLE mytable DROP COLUMN epochtime;
If I were you I'd try all this on a copy of your database to ensure it is correct before doing it on your production database.
If your epochtime values already have the TIMESTAMP data type, they are already stored internally as UTC (f/k/a GMT) times. The update operation I suggested will convert them to local time according to the settings on your server.
If your application has users in multiple time zones, you may wish to keep using the TIMESTAMP datatype: it honors time zone settings. If your epoch times are stored in an INT column, you can create your new column with the TIMESTAMP rather than DATETIME type by substituting this line for the first one in my instructions above.
ALTER TABLE mytable ADD COLUMN datestamp TIMESTAMP AFTER epochtime;
I have a table with a column containing unix time. I wish to create a new column that contains the day of the week for this time. For example, 1436160600 would be a Monday in this column.
I have created a new column, entitled "day_of_week"
alter table master add column test varchar(20);
I now wish to update this new column with the appropriate values.
I found the MySQL Unixtimestamp() function (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp)
I then attempted the following
update master set day_of_week = _sent_time_stamp(from_unixtime(unix_timestamp, %W));
where _sent_time_stamp is the column containing the Unix time values
But this results in an Error 1064.
Can anyone advise?
Solution. Convert epoch to date time
alter table master add column test_date datetime ;
update master set test_date = from_unixtime(_sent_time_stamp) ;
convert datetime to day of week using dayname function
alter table master add column test_day varchar(20) ;
update master set test_day = dayname(test_date) ;
I know this post is old, but the accepted answer is sadly wasteful, and I hope that future people seeking this answer may be more enlightened.
No need to add a new column to the table just for some temporary value. To achieve what you requested, you can simply do this:
UPDATE master
SET test_day = dayname(from_unixtime(_sent_time_stamp)) ;
However, even the goal is a wasteful in that we're simply storing two representations of the same data. What you can do instead, is to create a view:
CREATE VIEW master_vw AS
(SELECT mstr.*, DAYNAME(FROM_UNIXTIME(mstr._sent_time_stamp)) AS test_day
FROM master mstr) ;
Now, you can SELECT from this view anytime you like, and the value of test_day will always be in sync with the value of _sent_time_stamp. And no new column to maintain and whatnot.
There is a use case for actually storing the test_day column - execution of the view will take a miniscule amount of additional processing versus selecting from a table. And you cannot index over the virtual test_day column like you could in a table. So if you have millions of rows and you need to quickly get one that's (say) 'Saturday' then perhaps the table approach is more ideal.
But in cases where the column is just a convenience, or where it is simply a different representation of data that already exists, you'll do well to consider the View approach.
I am trying to convert my column into a DATE format.
It is currently in varchar and displays as: 12/06/2013
I run the following query;
UPDATE dispatch
SET dispatchdate = DATE_FORMAT(STR_TO_DATE(dispatchdate, '%d/%m/%Y'), '%d-%m-%Y');
ALTER TABLE dispatch
CHANGE dispatchdate dispatchdate date;
However after running this query, it displays my data as: 0000-00-00
I am trying to change it so that it will display as DD-MM-YYYY not YYYY-MM-DD
0000-00-00 is mysql's special way of displaying a "zero" or "dummy" date.
Like many of mysql's oddities, you learn to live with it and I suggest you:
UPDATE dispatch SET
dispatchdate = null
WHERE dispatchdate = '0000-00-00'
Or set to whatever value works best for you.
You'll have to build a scratch column: you cannot change the data type of a column in place, and expect to keep the data. It just won't work.
So
Add a new datetime column, maybe named tmp_dispatchdate
Use the query you've written to write the datetime value into that column
After confirming that the data converted correctly, drop dispatchdate
Rename tmp_dispatchdate to dispatchdate
If you have a primary key on the table in question, you can do all the "middle" work in a temp table, if you prefer, using the PK to reference the right record. But you'll still have to drop a column and add a column to do what you're trying to do.