Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 450
Ramakrishnan Avatar asked Mar 15 '11 13:03

Ramakrishnan


People also ask

How do I get milliseconds from SQL query?

We can use DATEPART() function to get the MILLISECOND part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as millisecond or mi .

Does Getdate () include time?

SQL Server GETDATE() FunctionThe GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.

What timezone does Getdate () use?

The difference between GETDATE() and GETUTCDATE() is in timezone, the GETDATE() function returns the current date and time in the local timezone, the timezone where your database server is running, but GETUTCDATE() return the current time and date in UTC (Universal Time Coordinate) or GMT timezone.


2 Answers

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.

like image 118
Adam Robinson Avatar answered Oct 09 '22 03:10

Adam Robinson


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

UPDATE

PRINT (CONVERT( VARCHAR(24), GETDATE(), 121)) 
like image 23
Kris Ivanov Avatar answered Oct 09 '22 03:10

Kris Ivanov