How to use UNIX_TIMESTAMP on a whole table? - mysql

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);

Related

convert multiple epoch time to date in sql together

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;

how to modify a column datatype in mysql so that it saves the previous records in the newly saved format

I just need to change the datatype of my column
however, I used the command
alter *table_name* modify column *column_name* *datatype* and it update the datatype but the previously saved result are in the same previous datatype . I want them to be modified too. Any help? And am I clear with my query?
It's been a while and I've been bouncing between database engines a lot lately so my syntax may be off, but the general idea will still be valid:
SELECT * INTO <table_name>_bak FROM <table_name>;
-- alter your table, leaving nulls in the column
UPDATE <table_name>
SET <column_name> = b.<column_name>
FROM <table_name> t
INNER JOIN <table_name>_bak ON <primary-key-join-clause-here>;
-- disable nulls in your modified column if needed
DROP TABLE <table_name>_bak;

Converting Epoch to day of the week in new column in MySQL

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.

Is it possible to see date of update of row mysql?

I'd like to see the update date per-row in a table (InnoDB) in mysql.
Is it possible? The only thing I found is the statistics on a table, not row.
SHOW TABLE STATUS
Any suggestions appreciated!
It is not possible, if you want to track the updates or inserts of 'row' in a table, you have to manually create a logic to do so, for example you can use triggers , to maintain the track of all changes and updates of the 'rows' in any other table.
I don't think you can see specific informations like this for a row.
What I usually do is that I create a column creation_date and modification_date in all my tables and then I fill them for each INSERT or UPDATE query with the function NOW()
You can also create your table this way :
CREATE TABLE [name]
[other colmns]
creation_date DATETIME DEFAULT CURRENT_TIMESTAMP
For this, see this topic.

Converting varchar to date displays 0000-00-00

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.