INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

User-Defined Functions

How to get Time Zone of the SQL Server by fhlee
Posted: 5 Mar 09

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:
CREATE VIEW dbo.SYS_GetServerTimeZone
AS
SELECT     DATEDIFF( hh, GETUTCDATE(), GETDATE() ) AS ServerTimeZone


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


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.
 

Back to Microsoft SQL Server: Programming FAQ Index
Back to Microsoft SQL Server: Programming Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close