在SQL Server我们一般使用GETDATE()来取得日期的信息,转成Varchar时会丢掉毫秒。
其实在SQL Server中还有很多函数可以取得日期信息:
SELECT SYSDATETIME() AS SYSDATETIME,
SYSDATETIMEOFFSET() AS SYSDATETIMEOFFSET,
SYSUTCDATETIME() AS SYSUTCDATETIME,
CURRENT_TIMESTAMP AS CURRENTTIMESTAMP,
GETDATE() AS GETDATE,
GETUTCDATE() AS GETUTCDATE;
查询结果如下:
SYSDATETIME:2012-06-08 11:31:51.3407471
SYSDATETIMEOFFSET:2012-06-08 11:31:51.3407471 +08:00
SYSUTCDATETIME:2012-06-08 03:31:51.3407471
CURRENTTIMESTAMP:2012-06-08 11:31:51.340
GETDATE:2012-06-08 11:31:51.340
GETUTCDATE:2012-06-08 03:31:51.340
如果用时间作为记录的Key,可以使用以下方式:
select replace(replace(replace(replace(SYSDATETIME(),'-',''),' ',''),':',''),'.','')