如何以毫秒为单位在 SQL Server 中打印 GETDATE()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5312205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print GETDATE() in SQL Server with milliseconds in time?
提问by Ramakrishnan
I want to print GETDATE()in SQL Server 2008, I need the time with milliseconds (this is for debugging purpose - to find sp's execution time )
我想在 SQL Server 2008 中打印GETDATE(),我需要以毫秒为单位的时间(这是为了调试目的 - 查找 sp 的执行时间)
I find this Difference
我发现这个区别
SELECT GETDATE()
returns 2011-03-15 18:43:44.100print GETDATE()
returns Mar 15 2011 6:44PM
SELECT GETDATE()
返回2011-03-15 18:43:44.100print GETDATE()
2011 年 3 月 15 日下午 6:44返回
I think SQL Server automatically typecast in print functionality.
我认为 SQL Server 会自动在打印功能中进行类型转换。
I need to print the date like this 2011-03-15 18:43:44.100
我需要像这样打印日期 2011-03-15 18:43:44.100
Thanks for your help.
谢谢你的帮助。
回答by Adam Robinson
First, you should probably use SYSDATETIME()
if you're looking for more precision.
首先,SYSDATETIME()
如果您正在寻找更高的精度,您可能应该使用。
To format your data with milliseconds, try CONVERT(varchar, SYSDATETIME(), 121)
.
要以毫秒为单位格式化数据,请尝试CONVERT(varchar, SYSDATETIME(), 121)
.
For other formats, check out the MSDN page on CAST
and CONVERT
.
对于其他格式,请查看和上CAST
CONVERT
的MSDN 页面。
回答by Kris Ivanov
SELECT CONVERT( VARCHAR(24), GETDATE(), 113)
UPDATE
更新
PRINT (CONVERT( VARCHAR(24), GETDATE(), 121))
回答by Stefan Michev
If your SQL Server version supports the function FORMAT you could do it like this:
如果您的 SQL Server 版本支持 FORMAT 函数,您可以这样做:
select format(getdate(), 'yyyy-MM-dd HH:mm:ss.fff')
回答by Marcello Miorelli
回答by S Kotra
This is equivalent to new Date().getTime()
in JavaScript :
这相当于new Date().getTime()
在 JavaScript 中:
Use the below statement to get the time in seconds.
使用以下语句以秒为单位获取时间。
SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint)
Use the below statement to get the time in milliseconds.
使用以下语句获取以毫秒为单位的时间。
SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint) * 1000
回答by admincedep
Create a function with return format yyyy-mm-hh hh:mi:ss.sss
创建一个返回格式为 yyyy-mm-hh hh:mi:ss.sss 的函数
create function fn_retornaFecha (@i_fecha datetime)
returns varchar(23)
as
begin
declare
@w_fecha varchar(23),
@w_anio varchar(4),
@w_mes varchar(2),
@w_dia varchar(2),
@w_hh varchar(2),
@w_nn varchar(2),
@w_ss varchar(2),
@w_sss varchar(3)
select @w_fecha = null
if ltrim(rtrim(@i_fecha)) is not null
begin
select
@w_anio = replicate('0',4-char_length( convert(varchar(4), year(@i_fecha)) )) + convert(varchar(4), year(@i_fecha)),
@w_mes = replicate('0',2-char_length( convert(varchar(2),month(@i_fecha)) )) + convert(varchar(2),month(@i_fecha)),
@w_dia = replicate('0',2-char_length( convert(varchar(2), day(@i_fecha)) )) + convert(varchar(2), day(@i_fecha)) ,
@w_hh = replicate('0',2-char_length( convert(varchar(2),datepart( hh, @i_fecha ) ) )) + convert(varchar(2),datepart( hh, @i_fecha ) ),
@w_nn = replicate('0',2-char_length( convert(varchar(2),datepart( mi, @i_fecha ) ) )) + convert(varchar(2),datepart( mi, @i_fecha ) ),
@w_ss = replicate('0',2-char_length( convert(varchar(2),datepart( ss, @i_fecha ) ) )) + convert(varchar(2),datepart( ss, @i_fecha ) ),
@w_sss = convert(varchar(3),datepart( ms, @i_fecha ) ) + replicate('0',3-DATALENGTH( convert(varchar(3),datepart( ms, @i_fecha ) ) ))
select @w_fecha = @w_anio + '-' + @w_mes + '-' + @w_dia + ' ' + @w_hh + ':' + @w_nn + ':' + @w_ss + '.' + @w_sss
end
return @w_fecha
end
go
Example
例子
select fn_retornaFecha(getdate())
and the result is: 2016-12-21 10:12:50.123
结果是:2016-12-21 10:12:50.123
回答by Lav
Try Following
尝试关注
DECLARE @formatted_datetime char(23)
SET @formatted_datetime = CONVERT(char(23), GETDATE(), 121)
print @formatted_datetime