以下查询返回13位数的时间戳。如何以正确的格式获取结果?

时间:2014-09-22 05:05:34

标签: mysql

我想获得CreatedTime& ResolvedTime以这种格式2014年9月1日上午11:49
但目前这两列的时间戳值是13位数,如1406878545884.

SELECT 
 qd.QUEUENAME "Group",
 wo.WORKORDERID "Request ID",
 aau.FIRST_NAME "Requester",
 std.STATUSNAME "Request Status",
 wof.UDF_CHAR16 "Status Reason",
 wo.TITLE "Subject",
 ti.FIRST_NAME "Technician",
 wo.CREATEDTIME "Created Time",
 wo.RESOLVEDTIME "Resolved Time" 
FROM WorkOrder wo 
 LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID 
 LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID 
 LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID 
 LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID 
 LEFT JOIN SDUser td ON wos.OWNERID=td.USERID 
 LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID 
 LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID 
 LEFT JOIN WorkOrder_Queue woq ON wo.WORKORDERID=woq.WORKORDERID 
 LEFT JOIN QueueDefinition qd ON woq.QUEUEID=qd.QUEUEID 
 LEFT JOIN WorkOrder_Fields wof ON wo.WORKORDERID=wof.WORKORDERID  
WHERE  
 (((((DATEDIFF(d, DATEADD(s,DATEDIFF(s,GETUTCDATE() ,GETDATE()) + (wo.CREATEDTIME/1000),'1970-01-01 00:00:00'),GETDATE()) <=14) 
AND 
 ((wo.CREATEDTIME != 0) 
AND 
 (wo.CREATEDTIME IS NOT NULL))) 
OR 
 ((DATEDIFF(d, DATEADD(s,DATEDIFF(s,GETUTCDATE() ,GETDATE()) + (wo.RESOLVEDTIME/1000),'1970-01-01 00:00:00'),GETDATE()) <=14) 
AND 
 ((wo.RESOLVEDTIME != 0) 
AND 
 (wo.RESOLVEDTIME IS NOT NULL)))) 
AND 
 (cd.CATEGORYNAME = N'Software Management' COLLATE Latin1_General_CI_AS)) 
AND 
 ((woq.QUEUEID IN (1201)) 
OR 
 ((wos.OWNERID = 13501) 
OR 
 (wo.REQUESTERID = 13501))))  
AND 
 wo.ISPARENT=1  
ORDER BY 1

2 个答案:

答案 0 :(得分:0)

这会将时间戳格式化为特定格式

select date_format(from_unixtime(1264105904),'%b %d, %Y %l:%i %p PDT');

输出将如下所示

Jan 21, 2010 8:31 PM PDT

编辑:

您的返回时间戳以毫秒为单位,因此使用上述格式化程序的结果为1000

select date_format(from_unixtime(1406878545884 / 1000),'%b %d, %Y %l:%i %p');

编辑2:

您的查询可以使用

SELECT qd.QUEUENAME "Group",
 wo.WORKORDERID "Request ID",
 aau.FIRST_NAME "Requester",
 std.STATUSNAME "Request Status",
 wof.UDF_CHAR16 "Status Reason",
 wo.TITLE "Subject",ti.FIRST_NAME "Technician",
 date_format(wo.CREATEDTIME,'%b %d, %Y %l:%i %p') "Created Time",
 date_format(wo.RESOLVEDTIME,'%b %d, %Y %l:%i %p') "Resolved Time" 
FROM WorkOrder wo
...

答案 1 :(得分:0)

改为使用longtodate。

SELECT qd.QUEUENAME "Group",
 wo.WORKORDERID "Request ID",
 aau.FIRST_NAME "Requester",
 std.STATUSNAME "Request Status",
 wof.UDF_CHAR16 "Status Reason",
 wo.TITLE "Subject",ti.FIRST_NAME "Technician",
 longtodate(wo.CREATEDTIME) "Created Time",
 longtodate(wo.RESOLVEDTIME) "Resolved Time" 
 FROM WorkOrder wo