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 use GetDate() or GetUTCDate() from a User Defined Function? by fhlee
Posted: 5 Mar 09

This FAQ is linked to my other FAQ which helps to return the SQL Server TIMEZONE from a User Defined Function.

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.

If you want to return the CURRENT GMT/UTC time on the SQL Server, you need to do:

1. You create VIEW first:
CREATE VIEW dbo.SYS_GetUTCDateTime
AS
SELECT     GETUTCDATE() AS UTCDateTime


2. You create a USER DEFINED FUNCTION:
CREATE FUNCTION dbo.fniGetUTCDateTime(  )
RETURNS DATETIME AS
BEGIN
    DECLARE    @vdUTC DATETIME
    SELECT @vdUTC = UTCDateTime FROM SYS_GetUTCDateTime
    RETURN @vdUTC
END


Replace the GetUTCDate() with GetDate() if you just want to return the current server local (system) time.
 

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