Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    205

    Unanswered: getdate() in user defined function

    Hi,

    It is possible to use getdate() in userdefined function. If so, how to do the same ?

    The following code throws error :

    create function function1
    return varchar
    DECLARE @currYYMM VARCHAR(20)
    SET @currYYMM = convert(char(4),getdate(),12)
    // Here it says the error 'getdate' can't be used inside functions
    ...............
    .....................

  2. #2
    Join Date
    Dec 2002
    Posts
    1,245
    If I recall correctly, a scalar user defined function must return a deterministic value (ie, if you pass in the same parameters, you will get the same results). A non-deterministic function, would take a randomizer (such as GetDate()) and return a different result everytime you called it (even when calling it with the same parameters). I don't believe that this is allowed.

    Regards,

    hmscott

    Quote Originally Posted by SamCute
    Hi,

    It is possible to use getdate() in userdefined function. If so, how to do the same ?

    The following code throws error :

    create function function1
    return varchar
    DECLARE @currYYMM VARCHAR(20)
    SET @currYYMM = convert(char(4),getdate(),12)
    // Here it says the error 'getdate' can't be used inside functions
    ...............
    .....................
    Have you hugged your backup today?

  3. #3
    Join Date
    Jul 2004
    Posts
    52
    Create view v_getdate as
    Select ThisDate = getdate()

    Then reference v_getdate.ThisDate in your function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •