Make a basic function

  • Hi,

    I need to make the following query into a function- I thought it'd be easy, but I keep getting errors. (maybe starting too late at night?)

    Can someone help me?  Thanks, J

    select

    cASE WHEN ( MONTH(GetDate())<6 and Day(GetDate())<16)

    then

    year(getdate())-1

    Else

    year(getdate()) END as 'year'

  • I got no errors when running what you posted.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Right- it works fine as a select statement.  I want to take it and turn it into a function so I only need to call it one time.

  • GETDATE() won't work inside an SS2000 function because of silly rules about being non-deterministic and all... you need to make a view for a simple GETDATE() ...

     CREATE VIEW dbo.Now

         AS

     SELECT GETDATE() AS Now

    Then, your code for the function would look like this...

     SELECT CASE

                WHEN MONTH(Now)<6 AND DAY(Now)<16

                THEN YEAR(Now)-1

                ELSE YEAR(Now)

            END AS 'YEAR'

       FROM dbo.Now

    I believe SS2005 allows GETDATE() in a function, though.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Ok... so did that do the trick for you or what?  A little feedback goes a long way around here...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Yes, thanks for your help- I did get it working.  Sorry it took so long to respond- I'm telecommuting after coming off maternity leave and am working some very whacky hours!

  • Heh... going back to the other posts, I see that now...

    Hope things are going well for Mother and Child.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply