我怎样才能始终以某种格式获取时间戳,而不必每次都明确定义它
How can I always get Timestamp in certain format without having to explicitly define it everytime
当我在时间戳上运行 select 时,我得到类似这样的信息:2015-01-02T23:22:36.000Z, 1/7/2015 11:03:31 AM
为了正确格式化,我必须这样做:
SELECT to_char(entered, 'MM/DD/YYYY HH12:MI:SS AM')
FROM analytics WHERE business_id = 371746;
甚至更糟:
var statement = "SELECT service_id, analytic_id, queues.business_id, name, queues.queue_id,";
statement += " to_char(serviced, 'MM/DD/YYYY HH12:MI:SS AM') as serviced,";
statement += " to_char(called, 'MM/DD/YYYY HH12:MI:SS AM') as called,";
statement += " to_char(entered, 'MM/DD/YYYY HH12:MI:SS AM') as entered ";
statement += " FROM queues INNER JOIN analytics";
statement += " ON queues.queue_id = analytics.queue_id";
statement += " AND queues.business_id = " + business_id + "";
statement += " AND line_id = " + line_id + " ORDER BY queues.queue_id";
这有点麻烦,因为我经常只做 SELECT *
而没有指定字段。
有没有办法告诉 Postgres 始终从 SELECT return 这种格式 'MM/DD/YYYY HH12:MI:SS AM'
而不必每次都指定 to_char?
这已经差不多了。
var statement = "
SET datestyle to SQL,MDY ;
SET lc_time to 'en_us.utf8' ;
SELECT ...
除了 en_us 似乎是 24 小时制。
您可以创建执行所有 to_char
转换的服务器端视图定义:
CREATE VIEW view_name AS SELECT
to_char(serviced, 'MM/DD/YYYY HH12:MI:SS AM') as serviced,
....
....
然后从 Javascript 将您的查询直接针对视图而不是具体的 table(或者从视图或 table 中手术选择列,具体取决于您如何定义视图).
当我在时间戳上运行 select 时,我得到类似这样的信息:2015-01-02T23:22:36.000Z, 1/7/2015 11:03:31 AM
为了正确格式化,我必须这样做:
SELECT to_char(entered, 'MM/DD/YYYY HH12:MI:SS AM')
FROM analytics WHERE business_id = 371746;
甚至更糟:
var statement = "SELECT service_id, analytic_id, queues.business_id, name, queues.queue_id,";
statement += " to_char(serviced, 'MM/DD/YYYY HH12:MI:SS AM') as serviced,";
statement += " to_char(called, 'MM/DD/YYYY HH12:MI:SS AM') as called,";
statement += " to_char(entered, 'MM/DD/YYYY HH12:MI:SS AM') as entered ";
statement += " FROM queues INNER JOIN analytics";
statement += " ON queues.queue_id = analytics.queue_id";
statement += " AND queues.business_id = " + business_id + "";
statement += " AND line_id = " + line_id + " ORDER BY queues.queue_id";
这有点麻烦,因为我经常只做 SELECT *
而没有指定字段。
有没有办法告诉 Postgres 始终从 SELECT return 这种格式 'MM/DD/YYYY HH12:MI:SS AM'
而不必每次都指定 to_char?
这已经差不多了。
var statement = "
SET datestyle to SQL,MDY ;
SET lc_time to 'en_us.utf8' ;
SELECT ...
除了 en_us 似乎是 24 小时制。
您可以创建执行所有 to_char
转换的服务器端视图定义:
CREATE VIEW view_name AS SELECT
to_char(serviced, 'MM/DD/YYYY HH12:MI:SS AM') as serviced,
....
....
然后从 Javascript 将您的查询直接针对视图而不是具体的 table(或者从视图或 table 中手术选择列,具体取决于您如何定义视图).