在 Microsoft SQL Server 2012 或更高版本中,是否可以在单个 select 语句中将日期时间值转换为 Unix 时间戳?如果是这样,怎么做?
最佳答案
正如 Peter Halasz 在 T-SQL DateTime to Unix Timestamp 中提到的那样:
Converting a datetime to unix timestamp is easy, but involves error prone typing the following:
@timestamp=DATEDIFF(second,{d '1970-01-01'},@datetime)
Where @datetime is the datetime value you want to convert. The {d ‘yyyy-mm-dd’} notation is an ODBC escape sequence.
The function:
CREATE FUNCTION UNIX_TIMESTAMP ( @ctimestamp datetime ) RETURNS integer AS BEGIN /* Function body */ declare @return integer SELECT @return = DATEDIFF(SECOND,{d '1970-01-01'}, @ctimestamp) return @return END
立即尝试如下@O A:
SELECT UNIX_TIMESTAMP(GETDATE());
关于sql - 将日期时间转换为 Unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455408/