Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Time Zone of the SQL Server

User-Defined Functions

How to get Time Zone of the SQL Server

by  fhlee  Posted    (Edited  )
I just wasted 3 hours trying to figure out how to RETURN the GMT Time Zone of my server in a User Defined function; so I am sharing this workaround.

Due to SQL determinism; If you try to use the GetDate() or GetUTCDate() function within a User Defined Function, you will get an error message:

Error 443: Invalid use of 'getdate' within a function.
-or-
Error 443: Invalid use of 'getutcdate' within a function.

The WORK AROUND:
1. First, you create VIEW first:
[color blue]CREATE VIEW dbo.SYS_GetServerTimeZone
AS
SELECT DATEDIFF( hh, GETUTCDATE(), GETDATE() ) AS ServerTimeZone
[/color]

2. Then, you create a USER DEFINED FUNCTION:
[color blue]CREATE FUNCTION dbo.fniGetServerTimeZone( )
RETURNS INT AS
BEGIN
DECLARE @viTZ INT
SELECT @viTZ = ServerTimeZone FROM SYS_GetServerTimeZone
RETURN @viTZ
END
[/color]

PERFORMANCE ISSUE: I am quite sure there is SOME performance issue associated with this. However, at least I get it to work. Comments/Improvement will be appreciated.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top