6

I can't seem to find an answer to this anywhere --- I want to convert a datetime in SQL to the excel serial number.

I'm essentially looking for the DATEVALUE function from excel but for use in SQL

Any ideas on how to do this? thanks

4

3 回答 3

8

假设所需日期是 2016-05-25

Select DateDiff(DD,'1899-12-30','2016-05-25')

退货

 42515

如果你也想要时间部分

Declare @Date datetime = '2016-05-25 20:00'
Select DateDiff(DD,'1899-12-30',@Date)+(DateDiff(SS,cast(@Date as Date),@Date)/86400.0)

退货

42515.8333333
于 2016-11-09T20:57:29.907 回答
0

您只需要将您的datetime价值转换为int并添加1

SELECT CONVERT(INT,YourDate) + 1
FROM dbo.SomeTable;
于 2016-11-09T21:00:16.510 回答
0

使用排序规则:SQL_Latin1_General_CP1_CI_AS

你应该使用

Select DateDiff(DD,'18991230','20160525')

返回 42515

您可以将 '20160525' 替换为 getdate() 或日期字段

于 2017-10-24T07:10:22.777 回答