mysql - 使用 sql 查询将元数据库中预配置的时间戳从 utc 格式转换为 ist

标签 mysql postgresql metabase

showing the initial timestamp format

目前我正在研究元数据库。我的时间戳类似于“2017 年 6 月 23 日星期五上午 8:43”。我需要将此时间戳从 utc 转换为 ist 形式。

代码是这样的

<小时/>
SELECT CAST("public"."locations"."timestamp" AS date) AS "timestamp"
FROM "public"."locations"
GROUP BY CAST("public"."locations"."timestamp" AS date)
ORDER BY CAST("public"."locations"."timestamp" AS date) ASC

最佳答案

在 PostgreSQL1 中,假设一个简化的场景,专注于将时间戳转换为 IST就是这个:

CREATE TABLE locations
(
    id serial PRIMARY KEY,
    your_timestamp timestamp with time zone
) ;

注意:不要使用timestamp作为列名称,以避免混淆。 timestamp已经是 type .

INSERT INTO locations
    (your_timestamp)
VALUES
    ('2017-01-01 10:00:00 UTC'),
    ('2017-01-01 10:00:00 Europe/Rome'),
    ('2017-01-01 10:00:00 America/New_York'),
    ('2017-01-01 10:00:00 IST') ;

您将转换所有这些时间(不仅是已经输入的 UTC 时间,或者连接到数据库的客户端的本地时区),您将使用:

SELECT
    id, 
    your_timestamp,
    your_timestamp AT TIME ZONE 'UTC' AS timestamp_utc,
    your_timestamp AT TIME ZONE 'IST' AS timestamp_ist
FROM
    locations ;
id | your_timestamp         | timestamp_utc       | timestamp_ist      
-: | :--------------------- | :------------------ | :------------------
 1 | 2017-01-01 10:00:00+00 | 2017-01-01 10:00:00 | 2017-01-01 12:00:00
 2 | 2017-01-01 09:00:00+00 | 2017-01-01 09:00:00 | 2017-01-01 11:00:00
 3 | 2017-01-01 15:00:00+00 | 2017-01-01 15:00:00 | 2017-01-01 17:00:00
 4 | 2017-01-01 08:00:00+00 | 2017-01-01 08:00:00 | 2017-01-01 10:00:00

您可以在 dbfiddle here 处查看。

<小时/>

引用文献:

<小时/>

1 我不知道 MySQL 的等效项。我希望您实际上正在使用 PostgreSQL,因为您的标识符是 "double quoted"而不是`backtick quoted`就像在 MySQL 中一样。

关于mysql - 使用 sql 查询将元数据库中预配置的时间戳从 utc 格式转换为 ist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379876/

相关文章:

postgresql - Heroku:无法使用配置数据库从外部连接到数据库

android - 如何访问从可穿戴 tizen 应用程序发送到 android 应用程序的字符串

php - 如何使用主键更新数据库

Python PostgreSQL 删除重复行

java - Windows 10 上的错误 : Unable to access jarfile metabase. jar

postgresql - 元数据库/Clojure 错误 : Unfreezable type: class org. postgresql.jdbc.PgArray

php - 如何在图表中显示每天的总订单金额

mysql - 无法在 FROM 子句中指定更新目标表 'tbl_prod'

sql - 字符不在轨道 UTF8 中

PostgreSQL:plv8.start_proc