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.
Related
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've been using CURRENT_TIMESTAMP in my DB for entries, but now wish to change to unix timestamp instead.
As seen here I have the data, but for each row I want to use UNIX_TIMESTAMP() and the value it returns to be placed in the unixtime column.
I've never messed around with big database changes and would appreciate a response.
I found it out myself by manipulating another post but doing it vice-versa.
UPDATE t_records SET unixtime = UNIX_TIMESTAMP(timeSet);
You might not able to replace the same column with unix_time, You will have to add another column first and set the values there. Later you can rename the columns.
alter table T add unix_time BIGINT(14);
update T set unix_time = unix_timestamp(<old_time_column>);
alter table T drop <old_time_column>;
alter table T change unix_time <old_time_column> BIGINT(14);
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.
I have two fields:
last_modified : datetime
updated_at : timestamp
updated_at is a new field which has just been added to the table, last_modified contains accurate dates.
I want to be able to loop through all of the rows in the table, selecting the last_modifed datetime, converting it into a timestamp and saving it in the new updated_at field.
Have no idea how to do this, any help is much appreciated!
Try
update table_name set updated_at=unix_timestamp(last_modified);
I don't why you want to store same data twice. Storing same data twice may result into inconsistency.
If you want timestamp in your code, you can do that in code itself.
If your table is called some_table, then you can put this in your migration's up class method:
execute 'UPDATE some_table SET updated_at = last_modifed'
MySQL should be able to convert from datetime to timestamp automatically. In an SQL SET clause, the columns are specific to a single row and the SET applies to all matched rows; without a WHERE clause the UPDATE applies to all rows in the table. Remember that SQL is set-based so everything operates on sets of rows and loops don't really fit the model.
I'm assuming that you're using ActiveRecord as you have a timestamp called updated_at. If you're not using ActiveRecord then you'll have to find another way to send the SQL UPDATE to the database.