I'm trying to convert a datetime from Asia/Manila to EST timezone
without declaring the exact interval like
date_sub(), subdate(), date_add(), adddate()
i find it easy to use
SELECT DATE_SUB('2016-04-04 13:00:00', INTERVAL 12 HOUR);
the result will be2016-04-04 01:00:00
But Im trying to create a dynamic script where i don't need to look how many hours is the difference between two timezone
and i find Convert_TZ() to do job
SELECT CONVERT_TZ('2016-04-04 13:00:00', 'Asia/Manila', 'EST');
but the result of this query is 2016-04-04 00:00:00
Maybe this native function is not including the "Daylight saving time(DST)"
Does anyone know how to do the trick?
where i can easily convert the time including the DST
to any timezone without hard coding the interval hour between the two timezone?
Thanks
Okay, my problem is solved, i use two option
First :
I simply use 'US/Eastern' not 'EST' to include the daylight in conversion.
Second:
Because I didn't know the first option earlier i do this to solve my problem at first.
I create a table that compose of the date where it is DST
which i found in some site online..
Then
I create a mysql function where its lookup to the table above
which if the specified date is between that DST Start and DST End it will automatically add 1 hour,
My function is like this,
CREATE FUNCTION usp_Convert(specified_date DATETIME, From_Timezone VARCHAR(20), To_Timezone VARCHAR(20), is_DST INT(1)) RETURNS datetime
DECLARE theDate DATETIME;
SET theDate = CONVERT_TZ(specified_date, From_Timezone, To_Timezone);
IF is_DST = 1 AND To_Timezone= 'EST' THEN
SET theDate = ADDDATE(theDate, INTERVAL 1 HOUR); END IF;
RETURN theDate;
This might not be the best answer but this totally solved my problem
Thanks.
Related
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;
I'm storing data like this:
Time - Date
13:20:20 - 2015-03-13
I want to select MySQL entries from within 10 minutes. So if I execute it 13:00 it would be 12:50-13:00.
I've spent ages researching how to use the -interval x minutes, (now) function etc, it just doesn't work... It might be that I'm storing the data badly (varchar type) or timezones issues with server provider
I'm thinking something like SELECT WHERE Time < - 60 seconds AND Date = '2015-03-13'
But it just doesn't work. All help appreciated here.
This is how you do it. Now don't do it. Seriously, have a look at how to do it below but go back and change the way you store dates to be a datetime type column. It makes your life easier and everyone who inherits the application from you (even if it's just a smarter you a year from now) won't hate you for making them do extra work.
create table mytmp
(
mydate varchar(10),
mytime varchar(8)
);
insert into mytmp
values ('2015-01-01', '01:23:45');
insert into mytmp
values ('2015-01-01', '01:24:45');
insert into mytmp
values ('2015-01-01', '01:25:45');
select *
from mytmp
where STR_TO_DATE(CONCAT(mydate, ' ', mytime), "%Y-%m-%d %H:%i:%S") > (now() - INTERVAL 10 minute);
select *
from mytmp
where STR_TO_DATE(CONCAT(mydate, ' ', mytime), "%Y-%m-%d %H:%i:%S") > (now() - INTERVAL 1 year);
drop table my tmp;
The key bit is to get the string date into a datetime format first using STR_TO_DATE. STR_TO_DATE needs a set format so that it can interpret what parts of the string represent what parts of a datetime type (minutes, days, years, etc) so you need to specify a format string. I used the CONCAT function to join your date and time columns into one string to do this. Now the INTERVAL operations will work as you're comparing like or compatible types.
You can find format strings for MySQL here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format)
Edit: The format string you want is '%H:%i:%s - %Y-%m-%d' by the looks.
So, I need to reset the expiration dates for a bunch of coupon codes in our database. Our expirations dates are field "to_date" and are displayed as the following: to_date = '2013-04-14'
I need to set the to_date as 28 days after the from_date. So basically, something like this:
UPDATE salesrule
SET name = 'New coupon code', to_date = 'from_date + 28 days'
I know this would work for a simple int value, but I'm not sure how to do this give that the data displays as an actual date. I have no control over how the date itself displays, that's a built in Magento functionality.
I'm a big noob when it comes to MySQL, but I've done some research and I've found the format function: FORMAT(Now(),'YYYY-MM-DD') I have a feeling this may be the key... can someone point me in the right direction it terms of formatting or writing this command correctly? Thank you!
UPDATE salesrule
SET name = 'New coupon code', to_date = DATE_ADD(from_date, INTERVAL 28 DAY);
More info about the DATE_ADD() function here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
to_date = DATE_ADD(from_date, INTERVAL 28 DAY)
Check this question out, it does what you want.
You can use the DATE_ADD() function:
... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE
It can also be used in the SELECT statement:
SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;
we have a store procedure, the IN parameter is DATE today. in this procedure, a aql is to compare this today value with a table which has a timestamp column.
for example:
column A
2012-12-01 00:00:00
SQL:
select * from t where A = today.
We run this procedure in phpmyadmin, it run OK. but it's not work in command line.
Why?
Guess you may need to format both dates into a common format.. To be safe you may even add Date() or str_to_Date if required...if you are not sure column A contains a proper date...
Try this please:
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(TODAY, '%d/%m/%Y');
if you meant CURDATE() by today then try this as well,
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(CURDATE(), '%d/%m/%Y');
It's possible that the dates are in different formats and that's causing them to be not equal. You can use datediff(date1, date2) = 0 to fix this.
http://www.w3schools.com/sql/func_datediff_mysql.asp
This returns 1 (aka TRUE)
SELECT DATE_SUB(NOW(), INTERVAL 24*100 HOUR) = DATE_SUB(NOW(), INTERVAL 100 DAY);
100 days ago, the hour of day does not change. But due to Daylight Savings Time (US), 100 twenty-four hour periods ago is actually one hour earlier than if you counted by days. If the above statement accounted for DST, it would return 0 or FALSE.
Is there a way I can say to account for DST for a given statement or session? I would prefer not to use UNIX_TIMESTAMP since it cuts off anything past 2038.
You'll need to create a custom function, something like this.
DELIMITER $$
CREATE FUNCTION DST(ADatetime DATETIME) RETURNS DATETIME
BEGIN
DECLARE result DATETIME;
SET Result = ADatetime;
IF ADatetime >= startDST AND ADateTime <= endDST THEN
result = DATE_SUB(ADatetime, INTERVAL 1 HOUR);
END IF;
RETURN result;
END $$
DELIMITER ;
This is really just a matter of converting to UTC and back:
CONVERT_TZ(DATE_SUB(CONVERT_TZ(NOW(),##session.time_zone,'UTC'), INTERVAL 24*100 HOUR),'UTC',##session.time_zone);
This assumes you have the timezone tables set up to use named time zones. If not, you can use '+0:00' instead of 'UTC'
How would cutting off anything past 2038 be a real problem when you can be sure that 64bit integer timestamps will be immplemented everywhere 20 years before that at least ?
Seriously, there are so many issues with the datetime / timestamp types in MySQL that you should try and avoid them when possible.
Do you store many dates beyond 2038 ?
And, why not try using PostgreSQL which has much more advanced type support ?