简体   繁体   English

如何根据列中当前存在的时间值从 UTC 转换为 EST?

[英]How would I convert from UTC to EST based on the time value that's currently present in the column?

I'd like to convert the time on both start and end columns from UTC to EST using SQL.我想使用 SQL 将startend列的时间从 UTC 转换为 EST。 I found the below query somewhere on the web after hours of searching for a way to do this but to no avail (the query below's incorrect).经过数小时寻找方法但无济于事后,我在 web 的某处找到了以下查询(以下查询不正确)。

Is there a clean way to convert from UTC to EST based on the date of the field?有没有一种干净的方法可以根据字段日期从 UTC 转换为 EST?

start column has the current value of 2019-06-01 05:00:00 start列的当前值为2019-06-01 05:00:00

end column has the current value of 2019-06-30 04:00:00 end列的当前值为2019-06-30 04:00:00

I'd like these to be in EST timezone.我希望这些在 EST 时区。

Thanks in advance.提前致谢。

UPDATE my_table
SET start = from_tz(to_timestamp('2009-11-17 18:40:05','yyyy-mm-dd hh24:mi:ss'), 'UTC'), end = from_tz(to_timestamp('2009-11-17 18:40:05','yyyy-mm-dd hh24:mi:ss'), 'UTC') 
AT TIME zone 'America/New_York'
WHERE id = 3;

You can use CONVER_TZ您可以使用 CONVER_TZ

you can use '+00:00' so the server hasn't to look it up, which will save time, like ysth explained in the comment您可以使用“+00:00”,这样服务器就不必查找它,这将节省时间,就像评论中解释的 ysth

 SELECT CONVERT_TZ('2007-03-11 2:00:00','UTC','America/New_York') AS time1, CONVERT_TZ('2007-03-11 3:00:00','UTC','America/New_York') AS time2;
 time1 |时间1 | time2:------------------ |:------------------ 2007-03-10 21:00:00 |时间2:----------------- |:----------------- 2007-03-10 21:00:00 | 2007-03-10 22:00:00 2007-03-10 22:00:00
 UPDATE my_table SET start = CONVERT_TZ('2009-11-17 18:40:05','UTC','America/New_York'), end = CONVERT_TZ('2009-11-17 18:40:05','UTC','America/New_York') WHERE id = 3;
 Table 'db_477873842.my_table' doesn't exist表 'db_477873842.my_table' 不存在
SELECT CONVERT_TZ('2007-03-11 2:00:00','+00:00','America/New_York') AS time1, CONVERT_TZ('2007-03-11 3:00:00','+00:00','America/New_York') AS time2;
 time1 |时间1 | time2:------------------ |:------------------ 2007-03-10 21:00:00 |时间2:----------------- |:----------------- 2007-03-10 21:00:00 | 2007-03-10 22:00:00 2007-03-10 22:00:00
 UPDATE my_table SET start = CONVERT_TZ('2009-11-17 18:40:05','+00:00','America/New_York'), end = CONVERT_TZ('2009-11-17 18:40:05','+00:00','America/New_York') WHERE id = 3;
 Table 'db_477873842.my_table' doesn't exist表 'db_477873842.my_table' 不存在

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM