How to print GETDATE() in SQL Server with milliseconds in time?

How to print GETDATE() in SQL Server with milliseconds in time?

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 )

I find this Difference

  • SELECT GETDATE() returns 2011-03-15 18:43:44.100
  • print GETDATE() returns Mar 15 2011 6:44PM

I think SQL Server automatically typecast in print functionality.

I need to print the date like this 2011-03-15 18:43:44.100

Thanks for your help.

回答1

First, you should probably use SYSDATETIME() if you're looking for more precision.

To format your data with milliseconds, try CONVERT(varchar, SYSDATETIME(), 121).

For other formats, check out the MSDN page on CAST and CONVERT.

回答2

SELECT CONVERT( VARCHAR(24), GETDATE(), 113)

UPDATE

PRINT (CONVERT( VARCHAR(24), GETDATE(), 121))
原文地址:https://www.cnblogs.com/chucklu/p/14962038.html