SQL Server 中的 UNIX_TIMESTAMP
我需要在 SQL Server 2008 中创建一个函数来模仿 mysql 的 UNIX_TIMESTAMP()
。
I need to create a function in SQL Server 2008 that will mimic mysql's UNIX_TIMESTAMP()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不关心 1970 年之前的日期或毫秒精度,只需执行以下操作:
几乎与 MySQL 的内置函数一样简单:
其他语言(Oracle、PostgreSQL 等): 如何获取当前纪元时间...
如果您需要毫秒精度(SQL Server 2016/13.x 及更高版本):
If you're not bothered about dates before 1970, or millisecond precision, just do:
Almost as simple as MySQL's built-in function:
Other languages (Oracle, PostgreSQL, etc): How to get the current epoch time in ...
If you need millisecond precision (SQL Server 2016/13.x and later):
试试这个帖子:
https://web.archive.org/web/20141216081938/http://skinn3r.wordpress.com/2009/01/26/t-sql-datetime-to-unix-timestamp/
或这篇文章:
http ://mysql.databases.aspfaq.com/how-do-i-convert-a-sql-server-datetime-value-to-a-unix-timestamp.html
代码如下:
示例用法:
Try this post:
https://web.archive.org/web/20141216081938/http://skinn3r.wordpress.com/2009/01/26/t-sql-datetime-to-unix-timestamp/
or this post:
http://mysql.databases.aspfaq.com/how-do-i-convert-a-sql-server-datetime-value-to-a-unix-timestamp.html
code is as follows:
Sample usage:
Sql Server 2016 及更高版本有一个 DATEDIFF_BIG 函数,可用于获取毫秒。
创建一个函数
并执行它
Sql Server 2016 and later have a DATEDIFF_BIG function that can be used to get the milliseconds.
Create a function
And execute it
对于毫秒结果的时间戳,我从这里找到了这个解决方案 https://gist.github.com/rsim/d11652a8336137832df9 :
@Rafe 的回答对我来说没有正确工作(MSSQL 20212) - 我有 9 秒的差异。
For timestamp with milliseconds result I found this solution from here https://gist.github.com/rsim/d11652a8336137832df9:
Answer from @Rafe didn't work for me correctly (MSSQL 20212) - I got 9 seconds of difference.
我经常需要一个毫秒精度的 unix 时间戳。下面将为您提供当前的unixtime
FLOAT
;包装上面的答案以获取函数或转换任意字符串。SQL Server 上的 DATETIME 数据类型仅适用于 3 毫秒,因此我为 SQL Server 2005 和 2008+ 提供了不同的示例。遗憾的是,没有
DATEDIFF2
函数,因此即使在 2008 年以上,也需要各种技巧来避免DATEDIFF
整数溢出。 (我不敢相信他们在没有修复这个问题的情况下引入了一个全新的DATETIME2
数据类型。)对于常规的旧
DATETIME
,我只是使用一个低级的强制转换来浮动,它返回 (floating点)自 1900 年以来的天数。现在我知道此时您在想闰秒怎么样?!?! Windows 时间和 unixtime 都不真正相信闰秒:对于 SQL Server 来说一天总是 1.00000 天长,对于 unixtime 来说一天总是 86400 秒长。 这篇维基百科文章讨论了unixtime在闰秒期间的行为方式;我相信 Windows 只是像任何其他时钟错误一样查看闰秒。因此,虽然当闰秒发生时两个系统之间不存在系统漂移,但它们在闰秒期间和之后的亚秒级别上不会达成一致。
FLOAT 实际上在 SQL Server 上默认为 8 字节双精度数,因此在许多用例中优于 32 位
INT
。 (例如,它们不会在 2038 年滚动。)I often need a unix timestamp with millisecond precision. The following will give you the current unixtime as
FLOAT
; wrap per answers above to get a function or convert arbitrary strings.The
DATETIME
datatype on SQL Server is only good to 3 msec, so I have different examples for SQL Server 2005 and 2008+. Sadly there is noDATEDIFF2
function, so various tricks are required to avoidDATEDIFF
integer overflow even with 2008+. (I can't believe they introduced a whole newDATETIME2
datatype without fixing this.)For regular old
DATETIME
, I just use a sleazy cast to float, which returns (floating point) number of days since 1900.Now I know at this point, you are thinking WHAT ABOUT LEAP SECONDS?!?! Neither Windows time nor unixtime really believe in leap seconds: a day is always 1.00000 days long to SQL Server, and 86400 seconds long to unixtime. This wikipedia article discusses how unixtime behaves during leap seconds; Windows I believe just views leap seconds like any other clock error. So while there is no systematic drift between the two systems when a leap second occurs, they will not agree at the sub-second level during and immediately following a leap second.
FLOATs actually default to 8-byte doubles on SQL Server, and therefore superior to 32-bit
INT
for many use cases. (For example, they won't roll over in 2038.)死灵术。
ODBC方式:
Necromancing.
The ODBC-way:
这是一个不声明任何函数或变量的单行解决方案:
Here's a single-line solution without declaring any function or variable:
如果您必须处理 SQL Server 的早期版本(<2016)并且您只关心正时间戳,我在这里发布了我为非常遥远的日期找到的解决方案(这样您就可以从 @rkosegi 的答案中删除 IF 。
什么)我所做的是首先计算天数差异,然后添加剩余秒数差异。
If you have to deal with previous versions of SQL Server (<2016) and you only care for positive timestamps, I post here the solution I found for very distant dates (so you can get rid of the IF from @rkosegi's answer.
What I did was first calculating the difference in days and then adding the difference in seconds left.
当调用标量函数时,可以使用以下语法
函数脚本:
调用函数:
When called to Scalar-valued Functions can use following syntax
Function Script :
Call Function :