SQL Server 中的 UNIX_TIMESTAMP [英] UNIX_TIMESTAMP in SQL Server
本文介绍了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()
.
提前致谢!
推荐答案
如果您不担心 1970 年之前的日期或毫秒精度,请执行以下操作:
If you're not bothered about dates before 1970, or millisecond precision, just do:
-- SQL Server
SELECT DATEDIFF(s, '1970-01-01 00:00:00', DateField)
几乎和 MySQL 的内置函数一样简单:
Almost as simple as MySQL's built-in function:
-- MySQL
SELECT UNIX_TIMESTAMP(DateField);
其他语言(Oracle、PostgreSQL 等):如何在 ...
Other languages (Oracle, PostgreSQL, etc): How to get the current epoch time in ...
如果您需要毫秒精度(SQL Server 2016/13.x 及更高版本):
If you need millisecond precision (SQL Server 2016/13.x and later):
SELECT DATEDIFF_BIG(ms, '1970-01-01 00:00:00', DateField)
这篇关于SQL Server 中的 UNIX_TIMESTAMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文