sql - 时间戳到日期 SQL

标签 sql sql-server sql-server-2008 timestamp google-bigquery

我需要像这样更改时间戳:

1519152103659000     
1519152113786000     
1519152118754001     
1519152118972002     
1519152119026003

到 sql 中的日期。这个时间戳是我表中的一列。我使用时间戳命令但出现错误:

错误:参数类型为 INT64 的函数 TIMESTAMP 没有匹配的签名。支持的签名:TIMESTAMP(STRING, [STRING]);时间戳(日期,[字符串]); TIMESTAMP(DATETIME, [STRING]) 在 [2:1]

谢谢!!

最佳答案

以下示例适用于 BigQuery StandardSQL

#standardSQL
WITH t AS (
  SELECT 1519152103659000 AS ts UNION ALL
  SELECT 1519152113786000 UNION ALL
  SELECT 1519152118754001 UNION ALL
  SELECT 1519152118972002 UNION ALL
  SELECT 1519152119026003 
)
SELECT 
  ts AS timestamp_in_microseconds_as_int64,  
  TIMESTAMP_MICROS(ts) AS timestamp_as_timestamp,
  DATE(TIMESTAMP_MICROS(ts)) AS dt
FROM t  

结果为

Row timestamp_in_microseconds_as_int64  timestamp_as_timestamp          dt   
1   1519152103659000                    2018-02-20 18:41:43.659 UTC     2018-02-20   
2   1519152113786000                    2018-02-20 18:41:53.786 UTC     2018-02-20   
3   1519152118754001                    2018-02-20 18:41:58.754 UTC     2018-02-20   
4   1519152118972002                    2018-02-20 18:41:58.972 UTC     2018-02-20   
5   1519152119026003                    2018-02-20 18:41:59.026 UTC     2018-02-20   

关于sql - 时间戳到日期 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48911191/

相关文章:

sql - 如何找出SQL Server执行计划中使用的中间值代表什么

sql - 获取 2 个提供日期之间的所有年份

sql-server - 如何防止sql存储过程中的重复更新

mysql - 哪个数据库 (DBMS) 最能处理大型表?

sql-server-2008 - 插入地理 : The specified input does not represent a valid geography instance because it exceeds a single hemisphere

mysql - 如何删除查询中的范围检查

mysql - phpMyAdmin 存储过程说明

sql-server - 在 sp_executesql 中使用 LIKE

php - 如何在php上获取正确的sql值

sql - SQL Server:如何合并来自同一表的两个选择查询并在列上获取结果