8

Below is what I pieced together but I wanted to see what other ways are available.

SET NOCOUNT ON;
GO
DECLARE @tz VARCHAR(50)
EXEC [master].[dbo].[xp_regread]
 'HKEY_LOCAL_MACHINE'
 ,'SYSTEM\CurrentControlSet\Control\TimeZoneInformation'
 ,'TimeZoneKeyName'
 ,@tz OUT;
SELECT 
 GETDATE()
 ,'(' + LEFT(PARSENAME(REPLACE(@tz, ' ','.'),3),1) 
 + '' + LEFT(PARSENAME(REPLACE(@tz, ' ','.'),2),1) 
 + '' + LEFT(PARSENAME(REPLACE(@tz, ' ','.'),1),1) +')'

Output: 2014年10月14日 16:22:21.037 (CST)

Colin 't Hart
9,51015 gold badges37 silver badges44 bronze badges
asked Oct 14, 2014 at 21:24
1
  • Your goal is to get the server's timezone? Commented Aug 13, 2015 at 22:35

1 Answer 1

4

Reading the reg key with this or SQLCLR is the only correct way I know (I personally would create a job which updates a table instead of enabling xp_regread though.).

This powershell script is an example of how to update a config table with this information.

$timeZone = (get-itemproperty 'HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\').TimeZoneKeyName
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server=.;database=myDatabase;trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = "
 MERGE ConfigTable AS target
 USING (SELECT 'TimeZone', @timeZone) AS source (ConfigKey, ConfigValue)
 ON (target.ConfigKey = source.ConfigKey)
 WHEN MATCHED THEN UPDATE SET ConfigValue = source.ConfigValue
 WHEN NOT MATCHED THEN INSERT (ConfigKey, ConfigValue) VALUES (source.ConfigKey, source.ConfigValue)" 
$command.Parameters.AddWithValue("@timeZone", $timeZone)
$Command.ExecuteNonQuery()
$Connection.Close()

All other systems with offset etc are giving incorrect results.

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
answered Aug 13, 2015 at 22:10
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.