MySQL Correct Usage of From_Unixtime in Trigger - mysql

I have the following trigger which inserts records into Table B whenever Table A is updated. This works fine however TableA_date is in unix time format and I want to convert it when the trigger inserts the record in Table B.
DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB SET
TableB_id = NEW.TableA_id,
TableB_date = FROM_UNIXTIME(NEW.TableA_date, '%d/%m/%y %r'),
TableB_comment = NEW.TableA_comment;
END $$
DELIMITER ;
In my results, instead of "01/01/70 03:00:05 AM" as the converted date I get "5" - I know the format string is correct as I am able to use it in a select statement. Thanks for your help

You are working too hard. Just leave out the format string entirely. Dates are stored as dates, not a strings.
In fact, don't even use the conversion at all! Just directly assign the date column, and MySQL will do any conversion it needs.
I'm assuming that TableA_date is stored using the timestamp datatype, and that TableB_date is datetime. (Although you may want timestamp for that one too.)
If you are using other datatypes like int, or char, then fix it. That's not the correct way to structure a database.
Just to answer your actual question (instead of telling you the correct way to do it) the date format you want is yyyy-mm-dd hh:mm:ss not what you have.

Related

Trigger not working to convert 'm/d/YYY' to DATE type. REPRO included

I'm going to be bulk inserting data from a file and I'd like to be able to convert the data type using MySQL instead of the backend language. Here's what I'm doing but it's not working and I can't figure it out. Thanks!
create table t1 (TEST_DATE DATE);
DELIMITER |
create trigger t1FixDate BEFORE INSERT ON t1
FOR EACH ROW BEGIN
SET NEW.TEST_DATE=STR_TO_DATE(NEW.TEST_DATE, '%c/%e/%Y');
END; |
DELIMITER ;
insert into t1 VALUES ('9/7/2016');
//Error Code: 1292. Incorrect date value: '9/7/2016' for column 'TEST_DATE' at row 1
edit: the below SQL works. I'm really stumped on this one. Are triggers done before type checking??
create table t2 (TEST_DATE DATE);
insert into t2 VALUES (STR_TO_DATE('9/7/2016', '%c/%e/%Y'));
According to the manual,
The keyword BEFORE indicates the trigger action time. In this case, the trigger activates before each row inserted into the table
So, trigger works with row, which had already been created, but not added to the table. It means, that your string was assigned to column with data type Date, this operation failed (null was assigned).
And after that trigger was called, so it deals with null value.

how to save only time not date in database through sql query

this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?
update table set current_time=now();
Your column must be set to either DATETIME or TIMESTAMP.
If you use the TIME type then your query would work as expected.
If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:
e.g. SELECT * FROM table WHERE current_time<'12:00'
You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql
U can use CONVERT (time, SYSDATETIME()) as the value.
This automates your process without using Current_time=now();
INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());
You can also use curtime();
INSERT INTO table SET current_time = curtime();
Credits: Salmaan C

MySQL - Trigger on Insert HOUR('time')

I have a table that contains a field 'timeduration'. Currently when I select the 'timeduration' value I'm doing HOUR('timeduration') to get the time in hours. I would like to do this calculation when inserting the data into the table, so putting the hour value into it's own column.
| date | timeduration | hour
12-12-14 01:00:00 1
I've looked into triggers but not sure how to about this, or os there an easier way I'm missing?
Thanks for your help!
Yes, a BEFORE INSERT and BEFORE UPDATE trigger would do it. And that's the only way to get MySQL to automatically populate the value in the hour column, based on the value assigned to the timeduration column.
Here's an example a BEFORE INSERT trigger.
DELIMITER $$
CREATE TRIGGER mytable_bi
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
SET NEW.hour = HOUR(NEW.timeduration);
END$$
DELIMITER ;
You'll likely also want a corresponding BEFORE UPDATE trigger, if you want to keep hour in sync when timeduration value is modified by UPDATE statement.

Issue of updating existing table with results of select statement for time zone conversion

I have a table called table1 with three columns, one of which is Date_Of_Call which is of datetime type with the data in PDT. I basically need to convert the data from PDT to UTC and put the UTC converted dates into a new column in the existing table. I added a new column with:
alter table table1 ADD Date_Of_Call_UTC DATETIME;
I am able to get the proper time conversion with this select statement:
select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1;
The issue I am having is trying to use an update command to take the results of the select statement and put them in the new Date_Of_Call_UTC column. Any thoughts of how to do this?
I tried the below statement and a few variations but can't quite figure out what I need to do:
update table1 set table1.Date_Of_Call_UTC = (select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1);
Any assistance is appreciated!
this one should work:
update table1
set table1.Date_Of_Call_UTC = CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00');
NOTE: dates are usually stored already as UTC in mysql, but during output they can be displayed with offset applied, read about it: http://dev.mysql.com/doc/refman/5.0/en/datetime.html and http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

Is it possible to create a column in MySQL with an expression as a default value?

How would one implement this in MySQL:
CREATE TABLE employee (
employeemonthly DECIMAL(10,2),
employeeyearly DECIMAL(10,2) DEFAULT employeemonthly*12
);
use a insert trigger for that. Something like this
DELIMITER |
CREATE TRIGGER default_yearly BEFORE INSERT ON employee
FOR EACH ROW BEGIN
SET NEW.employeeyearly = NEW.employeemonthly * 12;
END;
|
DELIMITER ;
I would use a view:
CREATE VIEW vemployees AS
SELECT e.employeemonthly,
e.employeemonthly * 12 AS employeeyearly
FROM EMPLOYEE e
...because there's little need to dedicate storage space for an easily calculated value. Otherwise, use a function or simply write the expression into whatever query/stored procedure you need.
What really depends is:
How often you need to access this data
How complex the operation is to get the result you need
I recommend starting with not storing the value. If performance gets to be a problem, then dedicate a column for the values storage -- not before. At that, a trigger is a bit overkill to me, when you can use (psuedocode):
INSERT INTO employee
(...employeemonthly, employeeyearly, ...)
VALUES
(...#employeemonthly, #employeemonthly * 12, ...
Use a trigger for the insert event, access the new record data using NEW and set the appropiate values.