row_to_json 上的时间戳格式

Gay*_*hri 4 postgresql json string-formatting

考虑tableA其中的列row_added_dttm类型为timestamp without timezone。实际值为2017-08-31 18:34:42.813175.

执行以下查询后,它会生成带有时区的时间戳,例如.{"crt_dttm":"2017-08-31T18:34:42.813175"}

select row_to_json(t) from (select row_added_dttm from tableA limit 1) as t;
Run Code Online (Sandbox Code Playgroud)

但我需要的格式类似于2017-08-31T18:34:42.813Z. 我不知道如何生成它,请帮忙。row_to_json需要使用。

Lau*_*lbe 5

无法影响 所使用的格式row_to_json

您可以在表上定义一个视图并使用

to_char(row_added_dttm, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
Run Code Online (Sandbox Code Playgroud)

将 格式化timestamp为字符串。

然后您可以使用row_to_json该视图来获得您想要的结果。

  • 感谢您的输入。我更改了查询以 select row_to_json(t) from (select to_char(row_added_dttm,'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') as row_added_dttm from tableA limit 1) as t; 这产生了所需的结果。 (2认同)