SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How long has passed since SQL Server service started?


How long has passed since SQL Server service started?

Author
Message
Lars Søe Mikkelsen
Lars Søe Mikkelsen
Right there with Babe
Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)

Group: General Forum Members
Points: 770 Visits: 493
Comments posted to this topic are about the item How long has passed since SQL Server service started?
jks-591682
jks-591682
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 84
Hi Lars. Very nice Scripts , if you could supply the startdate, ( SQL Server was started: Dec 15 2009 9:56AM Time since SQL Server service was started: 33 days 23 hours 17 minutes.) in the Output - It will fullfill it. Thanks :-)
Lars Søe Mikkelsen
Lars Søe Mikkelsen
Right there with Babe
Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)

Group: General Forum Members
Points: 770 Visits: 493
Hi jks-591682

To get the start time of the service, you could add this line:

PRINT 'SQL Server was started: ' + CONVERT(char,@starttime)


mchofman
mchofman
SSC-Addicted
SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)

Group: General Forum Members
Points: 495 Visits: 344
Lars,

you can also retrieve the start time from this Dynamic Management View

SELECT sqlserver_start_time FROM sys.dm_os_sys_info

(Requires VIEW SERVER STATE permission on the server)

Marco
Lars Søe Mikkelsen
Lars Søe Mikkelsen
Right there with Babe
Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)

Group: General Forum Members
Points: 770 Visits: 493
mchofman (2/4/2010)
Lars,

you can also retrieve the start time from this Dynamic Management View

SELECT sqlserver_start_time FROM sys.dm_os_sys_info

(Requires VIEW SERVER STATE permission on the server)

Marco


Hi Marco

That's correct for SQL Server 2008. The sqlserver_start_time in sys.dm_os_sys_info was added in SQL Server 2008 and wasn't there in SQL Server 2005.

Using the tempdb works for all our current versions Smile (yes, we still have SQL Server 2000)


Kind regards

Lars
mchofman
mchofman
SSC-Addicted
SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)SSC-Addicted (495 reputation)

Group: General Forum Members
Points: 495 Visits: 344
Lars,

I didn't know it isn't available in SQL 2005. Nice to know for a project I'm working. Thanks for your solution!

Regards
Marco
Satnam Singh
Satnam Singh
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1774 Visits: 1084
Fantastic....
henrik staun poulsen
henrik staun poulsen
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3298 Visits: 1246
Hej Lars,

For SQL Server 2008 you need to change line 1
from
SELECT crdate FROM sysdatabases WHERE name = 'tempdb'
to
SELECT create_date FROM sys.databases WHERE name = 'tempdb'

Med venlig hilsen
Henrik Staun Poulsen
Stovi Software



Lars Søe Mikkelsen
Lars Søe Mikkelsen
Right there with Babe
Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)

Group: General Forum Members
Points: 770 Visits: 493
Hi Henrik,

actually you don't need it, but I agree it would be better practice for SQL Server 2005 as well as SQL Server 2008 to use the sys.databases view which is available from SQL Server 2005 and forward.

Do do this while still maintaining the compatibility with SQL Server 2000, you could replace this:

-- determines when tempdb was created (done at startup)
DECLARE @starttime datetime
SET @starttime = (SELECT crdate FROM sysdatabases WHERE name = 'tempdb' )



With this:

-- determines when tempdb was created (done at startup)
DECLARE @starttime datetime
DECLARE @sql_version sysname
SET @sql_version = convert(sysname, (SELECT SERVERPROPERTY('Productversion')))

IF (@sql_version NOT LIKE '8.%')
BEGIN
-- SQL Server 2005 and forward
SET @starttime = (SELECT create_date FROM sys.databases WHERE name = 'tempdb')
END
ELSE
BEGIN
-- SQL Server 2000
SET @starttime = (SELECT crdate FROM sysdatabases WHERE name = 'tempdb')
END



The premises for this however is that you don't execute it on versions older than SQL Server 2000, but I hope you don't have any of them running anyway :-)


Best regards

Lars Søe Mikkelsen
JN Data
scott.pletcher
scott.pletcher
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2158 Visits: 473
Very nice! You can shorten it up a bit though :-)


DECLARE @minutesSinceSQLStarted int

SELECT @minutesSinceSQLStarted = DATEDIFF(MINUTE,
-- determines when tempdb was created (done at startup)
(SELECT crdate FROM master.dbo.sysdatabases WHERE name = 'tempdb'), GETDATE())

PRINT 'Time since SQL Server service was started: ' +
CONVERT(varchar(4), @minutesSinceSQLStarted / (60*24)) + ' days ' +
CONVERT(varchar(2), @minutesSinceSQLStarted % (60*24) / 60) + ' hours ' +
CONVERT(varchar(2), @minutesSinceSQLStarted % (60*24) % 60) + ' minutes.'



Scott Pletcher, SQL Server MVP 2008-2010
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search