I have a MySQL table that has date field in UTC default format. I want to also store the same date/time in PST format in another column in same table. Is it possible to do it in an SQL?
You can use convert_tz to do conversion between different time zone.
mysql> select convert_tz('2016-03-16 7:00:00', '+00:00','-08:00');
+-----------------------------------------------------+
| convert_tz('2016-03-16 7:00:00', '+00:00','-08:00') |
+-----------------------------------------------------+
| 2016-03-15 23:00:00 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
To do the task you mentioned in your post, just use a UPDATE as below
update tbl set col2 = convert_tz(col1, '+00:00','-08:00');
I guess I figured it out. Using DATE_SUB(datetimeutc,INTERVAL 8 HOUR) resolved my problem.
Related
There is a problem when using Mysql to extract data: negative timestamp cannot pass FROM_ UNIXTIME method converted to normal date:
FROM_UNIXTIME(-2641363543)
Null
I know the correct date corresponding to this timestamp is: 1886-04-20 00:00:00. I searched for it, and some people suggested that can be calculated in a relative way:
DATE_ADD(FROM_UNIXTIME(0), INTERVAL -2641363543 SECOND)
1886-04-19 23:54:17
But there is a few minutes gap between the calculated period and the correct value. I don't know what the problem is and what the correct method should be.
It's pretty clear just from looking at it that -2641363543 is not midnight. If subtracted from 1970-01-01 00:00:00 it would need to end with a zero.
According to both Ruby and Perl it is 1886-04-19 15:54:17 UTC.
> Time.at(-2641363543).utc
=> 1886-04-19 15:54:17 UTC
You want -2641334400.
> Time.gm(1886, 4, 20).to_i
=> -2641334400
And the technique does work.
mysql> set time_zone = '+00:00';
Query OK, 0 rows affected (0.01 sec)
mysql> select FROM_UNIXTIME(0) + INTERVAL -2641334400 SECOND;
+------------------------------------------------+
| FROM_UNIXTIME(0) + INTERVAL -2641334400 SECOND |
+------------------------------------------------+
| 1886-04-20 00:00:00 |
+------------------------------------------------+
Which brings us to perhaps the real problem...
the correct date corresponding to this timestamp is: 1886-04-20 00:00:00
Correct for which time zone?
I wrote a sql to calculate time diff between now and last updated time. Firstly I just use CURRENT_TIMESTAMP - updated_time and found the result looks like correct in time unit second. But it wasn't stable, sometimes the result went to much bigger that correct one. And then I changed to TIMESTAMPDIFF(SECOND, updated_time, CURRENT_TIMESTAMP ) , everything is OK. My question is what's the difference of tow expressions?
The CURRENT_TIMESTAMP() or CURRENT_TIMESTAMP are synonyms for NOW() which gives your current time.
Edit2:
After your additional comment I understood what you are asking. (I have deleted the first edit) which was incomplete and somewhat incorrect.
The question is: "To explain inner workings of CURRENT_TIMESTAMP - updated_time."
The explanation (I went way deeper):
The CURRENT_TIMESTAMP can return date and time in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format.
What maybe confused you is that it can return either string or numeric value based on the context.
Here you have a numeric context as you have the - (minus) operator.
`String context`
SELECT CURRENT_TIMESTAMP();
-> '2017-07-04 08:50:26'
OR
`numeric context`
SELECT NOW() + 0;
-> 20170704085026
The - (minus) operator only appears to work:
mysql> insert into temp (first, second)
-> VALUES ('2017-07-01 03:00:00', '2017-07-01 03:01:00');
Query OK, 1 row affected (0.00 sec)
mysql> select first, second, second - first from temp;
+---------------------+---------------------+----------------+
| first | second | first - second |
+---------------------+---------------------+----------------+
| 2017-07-01 03:00:00 | 2017-07-01 03:00:37 | 37.000000 |
| 2017-07-01 03:00:00 | 2017-07-01 03:01:00 | 100.000000 |
+---------------------+---------------------+----------------+
2 rows in set (0.00 sec)
Oh nice! 100 seconds in a minute? I don't think so! :).
To correctly subtract your time (if updated_time is in seconds):
The TIME_TO_SEC is needed: TIME_TO_SEC(CURRENT_TIMESTAMP) - updated_time
I have migrated data from old DB to new DB where date was taken as varchar, I have migrated data successfully but problem in date . In old DB the format of date is 25-01-02 where 25 is day 01 is month and 02 is year. But my script converted it to 2025-01-02. How can I fix it in my SQL?
TEST case:
DATE OUTPUT
2025-05-01 2001-05-25
2002-08-16 2016-08-02
2031-01-01 2001-01-31
2028-08-16 2016-08-28
2001-05-01 2001-05-01
You can it easy convert with STR_TO_DATE like this:
SELECT STR_TO_DATE('25-01-02', '%d-%m-%y');
sample
mysql> SELECT STR_TO_DATE('25-01-02', '%d-%m-%y');
+-------------------------------------+
| STR_TO_DATE('25-01-02', '%d-%m-%y') |
+-------------------------------------+
| 2002-01-25 |
+-------------------------------------+
1 row in set (0,00 sec)
mysql>
See the Manual: https://mariadb.com/kb/en/mariadb/str_to_date/
I used MYSQL TIME() function to change the time format. But it gives only the time, not recognize the am or pm.
I got 01:30:00 from TIME('01:30 pm'). I want to achieve it only in MYSQL. No PHP or others. Please advice.
Thats not a real time you are storing in mysql since you also have am/pm
You first need to convert to real datetime using str_to_date and then use time_format
mysql> select time_format(str_to_date('01:30 pm','%h:%i %p'),'%H:%i:%s') as time ;
+----------+
| time |
+----------+
| 13:30:00 |
+----------+
1 row in set (0.00 sec)
Use this function TIME_FORMAT(time,format)
Ok, so the following works fine
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
But if I give only a date argument, like in:
mysql> SELECT UNIX_TIMESTAMP('2007-11-30');
Then somehow I am getting the timestamp equivalent to 2007-11-30 18:30 GMT. Can I somehow reset it to give timestamp for the beginning of that particular day? Like UNIX_TIMESTAMP('2007-11-30'); should give the timestamp equivalent of UNIX_TIMESTAMP('2007-11-30 00:00:00'); I need to filter out some records from a table based an event that happend after a certain date.
Thanks
[EDIT]: I don't know how but this seems to be working as expected now. Screenshots: 2007-11:30 00:00:00 2007-11:30 18:30:00 2007-11:30
I have checked it But i am getting same timestamp for '2007-11-30 00:00:00' and '2007-11-30'
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 00:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('2007-11-30 00:00:00') |
+---------------------------------------+
| 1196361000 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT UNIX_TIMESTAMP('2007-11-30');
+------------------------------+
| UNIX_TIMESTAMP('2007-11-30') |
+------------------------------+
| 1196361000 |
+------------------------------+
Can you run these queries on your machine and check timestamp values.
I also checked, for me as well it is giving for 00:00:00. On further investigation, I came across this Here:
The server interprets date as a value in the current time zone and
converts it to an internal value in UTC. Clients can set their time
zone as described in Section 10.6, “MySQL Server Time Zone Support”.
On further searching, it became apparent that there is a variable system_time_zone, that is set when server starts using server machine's timezone. See here also
For each client connecting, they can set their own timezone as
mysql> SET time_zone = timezone;
So finally, you need to check your system_time_zone, set it to proper value.
I hope it will work well then....
Found a way to make sure, that the UNIX_TIMESTAMP function returns the correct timestamp irrespective of anything interfering with the timezone etc
>> SELECT UNIX_TIMESTAMP(CONCAT(t.DATE,' 00:00:00')) FROM db.table_name t
That is, to append the 00:00:00 manually in the query string.