将历元时间戳转换为sql server(人类可读格式)

时间:2021-12-02 23:41:40

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表格中的数据,我将通过一个工具导入这些数据。因此,我正在寻找一种代码或语法,可以将历元时间戳转换为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
  • 时代的时间戳,-1291388960
  • sql server timestamp--- 2010-12-03 15:09:20.000
  • sql server时间戳——- 2010-12-03 15:09:20.000

2 个解决方案

#1


18  

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列不能同时是datetime列,所以不能更新。您必须将它们更新为另外3列。

#2


14  

Use the DATEADD function:

使用返回函数:

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 CONVERT to format the date in TSQL.

DATEADD将返回一个DATETIME数据类型,因此如果您已经建立了表和列——您可以根据需要使用该函数插入/更新。提供细节,我会澄清。有了DATETIME之后,可以使用CAST或CONVERT来格式化TSQL中的日期。

#1


18  

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列不能同时是datetime列,所以不能更新。您必须将它们更新为另外3列。

#2


14  

Use the DATEADD function:

使用返回函数:

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 CONVERT to format the date in TSQL.

DATEADD将返回一个DATETIME数据类型,因此如果您已经建立了表和列——您可以根据需要使用该函数插入/更新。提供细节,我会澄清。有了DATETIME之后,可以使用CAST或CONVERT来格式化TSQL中的日期。