Can default value for datetime field be expression? - mysql

MYSQL server version: 5.7.25.
I have table where default value for dt field is CURRENT_TIMESTAMP. Today found, it adding dates in UTC, and as found from forums, in order to have date in PDT, need add trigger. is following correct?
BEFORE INSERT ON portin_failed_email_sent
FOR EACH ROW SET new.dtg_pdt = IFNULL(new.dtg_pdt,DATE_SUB(new.dt, INTERVAL 7 HOUR ));

Related

Looking for MySQL Default yesterday date

I am using MySql via terminal. Below is the command I have used to create table but it is showing date in YYYY-MM-DD HH:MM:SS (example: 2018-05-25 14:12:47)
create table test (foo int, ts timestamp default CURRENT_TIMESTAMP);
But I want by default it take yesterday date every time I insert data in (YYYY-MM-DD) format.
Please help me to find the command.
Thanks,
Amit
According to the official MySQL documentation https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add, you can do like this:
If you want to store the "yesterday" on creation:
ts TIMESTAMP NOT NULL DEFAULT NOW() - INTERVAL 1 DAY
If you want to store the "yesterday" on every update:
ts TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() - INTERVAL 1 DAY
According to this answer Select records from NOW() -1 Day:
NOW() returns a DATETIME.
And INTERVAL works as named, e.g. INTERVAL 1 DAY = 24 hours.
So if your script is cron'd to run at 03:00, it will miss the first
three hours of records from the 'oldest' day.
To get the whole day use CURDATE() - INTERVAL 1 DAY. This will get
back to the beginning of the previous day regardless of when the
script is run.
Hope it helps!
DEFAULT values in MySQL must be constants. They can't be functions or expressions (with the exception of CURRENT_TIMESTAMP).
Source: http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
In addition you can add a trigger to your table for your requirement
Simply Create a Table without constraint
create table test (foo int, ts timestamp );
Then add a trigger to this table
CREATE TRIGGER settime
BEFORE INSERT on test
FOR EACH ROW BEGIN
IF new.`ts ` is null THEN
SET new.`ts ` = DATE_ADD(NOW(), INTERVAL -1 DAY);
END IF;
END;

How to add 1 hour to currrent_timestamp in mysql which is the default value?

I have a database with timestamp field which takes current timestamp by default, but I have problem with the time, like if I insert the row at 9:00 it will take 8 as timestamp.
So my question is how to make current_timestamp in that table add one hour by default? I know you can do it with php but I prefer pure mysql solution.
I have a problem with the server timezone but I don't want to change it, since I am afraid this might affect other databases on server, while I want to change timestamp only in one database.
Simply you cannot do CURRENT_TIMESTAMP + INTERVAL 1 HOUR, but you can define a trigger instead:
CREATE TRIGGER tr_dt_table BEFORE INSERT ON your_table FOR EACH ROW BEGIN
SET NEW.datetime_field = NOW() + INTERVAL 1 HOUR;
END
And remove any default values of that field (i.e. make it NULL by default) in order to avoid contradictions.
insert into table_name values (DATE_ADD(now() , INTERVAL 1 HOUR));

Timestamp gets updated with zero

I need to update a column with datatype as timestamp. I run the following query:
update job_info set ProcessStartTime = UNIX_TIMESTAMP(CURDATE()) where JobID=4;
But it updates with a value : 0000-00-00 00:00:00 What could be the reason for this? Is the query incorrect?
Don't use UNIX_TIMESTAMP because MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds or your column type is datetime
update job_info set ProcessStartTime =CURDATE() where JobID=4;
or use NOW()
update job_info set ProcessStartTime =NOW() where JobID=4;

Mysql - Update the timestamp

My timestamp is 8 hours behind my local time so typically
2015-08-24 12:50:07
Can I update this to
2015-08-24 20:50:07
Try to use field of datetime type instead and obtain the value by function NOW()

Is A Datetime Column's Value the Default One

I have a MySQL table and a column of type datetime. It's default value is something like 0000-00-00 00:00:00.
How do I check if a given row's value on this datetime column is the above by using native MySQL query functionality. E.g. without using "SELECT * FROM table WHERE my_date<>'0000-00-00 00:00:00'", because this leaves room for errors on different MySQL servers and configurations I believe.
SELECT * FROM table WHERE my_date <> 0
You can test it with
select cast('0000-00-00 00:00:00' as datetime) = 0
which returns true (and false for all other datetime values).
You could do something like "SELECT * FROM table WHERE my_date > '1970-01-01 00:00:00". 1/1/1970 is the commonly used epoch date.