将纪元时间戳转换为 sql server(人类可读格式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4787827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting Epoch timestamp to sql server(human readable format)
提问by Shahsra
I have a problem in converting the Unix timestamp to sql server timestamp.
我在将 Unix 时间戳转换为 sql server 时间戳时遇到问题。
I have a data in excel sheet and I will import that data through a tool. So I am looking for a code or syntax which can convert that Epoch timestamp to sql server timestamp.
我在 excel 表中有一个数据,我将通过工具导入该数据。因此,我正在寻找一种代码或语法,可以将该 Epoch 时间戳转换为 sql server 时间戳。
I have 3 different columns with the same format. How can I change the values in those columns.
我有 3 个不同的列,格式相同。如何更改这些列中的值。
For Example:
例如:
- Epoch timestamp ---1291388960
- sql server timestamp--- 2010-12-03 15:09:20.000
- 纪元时间戳---1291388960
- sql server 时间戳--- 2010-12-03 15:09:20.000
采纳答案by RichardTheKiwi
I have 3 different columns with the same format. How can I change the values in those columns.
我有 3 个不同的列,格式相同。如何更改这些列中的值。
To update 3 columns in a table, you can pair DATEADD seconds to the epoch (1 Jan 1970) with the column name, i.e.
要更新表中的 3 列,您可以将 DATEADD 秒与列名(1970 年 1 月 1 日)配对,即
update tbl set
datetimecol1 = dateadd(s, epochcol1, '19700101'),
datetimecol2 = dateadd(s, epochcol2, '19700101'),
datetimecol3 = dateadd(s, epochcol3, '19700101')
You can't update in place since a bigint column cannot also be a datetime column. You have to update them into 3 other columns.
您无法就地更新,因为 bigint 列不能同时是日期时间列。您必须将它们更新为 3 个其他列。
回答by OMG Ponies
Use the DATEADD function:
使用DATEADD 函数:
SELECT DATEADD(ss, 1291388960, '19700101')
...specifying a date of January 1st, 1970. In this example, it was provided in the YYYYMMDD format.
...指定 1970 年 1 月 1 日的日期。在此示例中,它以 YYYYMMDD 格式提供。
DATEADD will return a DATETIME data type, so if you have a table & column established -- you can use the function to INSERT/UPDATE depending on your needs. Provide details, and I'll clarify. Once you have a DATETIME to work with, you can use CAST or CONVERTto format the date in TSQL.
DATEADD 将返回 DATETIME 数据类型,因此如果您建立了表和列 - 您可以根据需要使用该函数插入/更新。提供细节,我会澄清。一旦有了要使用的 DATETIME,就可以使用CAST 或 CONVERT在 TSQL 中格式化日期。