Important alert: (current site time 7/15/2013 7:56:10 PM EDT)
 

article

Find out when a SQL Server Instance Started with udf_SQL_StartDT

Email
Submitted on: 5/21/2003 8:33:27 AM
By: Andrew Novick  
Level: Intermediate
User Rating: Unrated
Compatibility: SQL Server 2000
Views: 8721
author picture
(About the author)
 
     Sometimes you need to know when the Instance of SQL Server started. There are manual methods to find out but this article shows you a UDF that gets the information.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Sometimes you want to know when the SQL Server Instance was
started.  You'll find an event in the NT event log, and a new
SQL Server Log file is created each time the instance starts so
you could easily look in Enterprise Manager to figure out when
it started.

If you want to know this information from inside T-SQL it's a
little bit trickier.  I recently had to find this information
for a client.  I located the information in a 1998 posting in
the microsoft.public.sqlserver.programming newsgroup by
Med Bouchenafa of France.

SQL Server won't tell you it's start time directly but you can
query the login time of one of the system processes.  Since
they start when the instance starts the process's login time
is a good proxy for when the SQL Server instance started.

CREATE FUNCTION dbo.udf_SQL_StartDT ()

    RETURNS datetime -- Date/time the SQL Server Instance started
/*
* Returns the data/time that the SQL Server instance was started.
*
* Example:
select dbo.udf_SQL_StartDT() as [System Started AT]
*
* © Copyright 2003 Andrew Novick http://www.NovickSoftware.com
* You may use this function in any of your SQL Server databases
* including databases that you sell, so long as they contain
* other unrelated database objects. You may not publish this
* UDF either in print or electronically.
*
* Published as Vol 1 #11 of The T-SQL UDF of the Week Newsletter
http://www.NovickSoftware.com/UdfofWeek/UDFofWeek.htm
****************************************************************/
AS BEGIN

    DECLARE @WorkingVariable datetime

    SELECT @WorkingVariable = login_time
        FROM master..sysprocesses
        WHERE cmd='LAZY WRITER'

    Return @WorkingVariable
END


GRANT  EXECUTE  ON [dbo].[udf_SQL_StartDT]  TO [PUBLIC]
GO


So let's try it:

select dbo.udf_SQL_StartDT() as [System Started AT]
GO

(Results)
System Started AT
------------------------------------------------------
2003-01-23 11:40:37.283
(End of results)

After I wrote udf_SQL_StartDT I thought of one other way to get
the system up time.  The built-in system function
fn_virtualfilestats returns a Timestamp column that is the number
of milliseconds since the system come up.  

The next query uses SQL Server's MIN aggregate function on the
Timestamp column because fn_virtualfilestats returns the same
value for every row.  It then converts it to seconds and
subtracts it from the current time to compute the start time of
the SQL Server instance:

SELECT DATEADD(ss, -1 * min(Timestamp)/1000 , getdate())
                  AS [Start per fn_virtualfilestats]
    , dbo.udf_SQL_StartDT()
                  AS [Start per udf_SQL_StartDT]
   FROM ::fn_virtualfilestats(-1, -1)
GO

(Results)
Start per fn_virtualfilestats  Start per udf_SQL_StartDT      
------------------------------ ------------------------------
2003-01-23 11:38:50.200        2003-01-23 11:40:37.283
(End of results)

As you can see, there's a minute and 47 second difference between
the two times.  Which one is better?  It's possible to make a
case for either number.  I'll stick with the later one based on
the reasoning that "While the SQL Server might have been starting
earlier, no application work was done before the LAZY WRITER
process's start time so that's a better indication of when the
system was available."


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.