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;
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 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.
MySQL has a data type called timestamp. This is a date time field that is updated to now() when any data in the record is changed.
How can I achieve the same behaviour in SQL Server?
I know about the rowversion data type in SQL Server. I don't want this - I want an auto-updating date time value.
Create an INSTEAD OF INSERT trigger, in which you insert into your table, selecting all the columns from INSERTED except for the date time column you want to always reflect the current date time, and insert GETDATE() in for that column.
Then create an INSTEAD OF UPDATE trigger, and do something similar, running an UPDATE statement against the table from inserted, using GETDATE() for that specific column.
To emulate MySQL's TIMESTAMP column you'll need a DATETIME2(7) column and an INSTEAD OF UPDATE trigger. And use the SYSDATETIME function to populate the datetime2 column.
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 a timestamp in a database table. Now everytime a record is amended the timestamp changes - this isn't really what I want as the record represents a sale so everytime the data is amended it looks like the sale time has changed! Thus I have added a new field to the database table called 'sale_date' and I want to select the timestamp field of that record, called 'sale_time' and convert the timestamp to the format dd/mm/yyyy hh:mm:ss. and insert it into the new 'sale_date' field (which is text not date)
Any ideas? I'm rubbish at mysql.
If I haven't explained myself well please say.
The definition of your timestamp column (one that changes on updates) constains ON UPDATE CURRENT_TIMESTAMP clause. Remove it (ALTER TABLE) and it will stop updating.
And please, pleasee, please, do not ever store dates as text.