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
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
假设所需日期是 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
您只需要将您的datetime
价值转换为int
并添加1
:
SELECT CONVERT(INT,YourDate) + 1
FROM dbo.SomeTable;
使用排序规则:SQL_Latin1_General_CP1_CI_AS
你应该使用
Select DateDiff(DD,'18991230','20160525')
返回 42515
您可以将 '20160525' 替换为 getdate() 或日期字段