how to write a function

  • i want to pass datetime to a functon e.g

    @startdate,@enddate

    the 2 params would look into a table and bring out a column for example "price" for the dates between the 2 dates passed in. 

    Any ideas?

  • There is a topic "CREATE FUNCTION" in BOL.

    There are some examples down there.

    What's not clear for you in this topic?

    _____________
    Code for TallyGenerator

  • Yeah went through some tutorials.

    Will post back if need some help

    Thx

  • Try this

    CREATE FUNCTION TEST_1 (@SDate DateTime,@EDate DateTime)

    RETURNS TABLE

    AS

    RETURN (SELECT price

          FROM X

          WHERE Convert(Varchar(12),StartDate,112) BetWeen Convert(Varchar(12),@SDate,112)

      And Convert(Varchar(12),@EDate,112)

  • Why you need CONVERT?

    Just to avoid using index and make this query heavy?

    _____________
    Code for TallyGenerator

  • Better than that, why do you think you need a function at all?  *sniff* *sniff* I smell some RBAR coming up

    --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 6 posts - 1 through 5 (of 5 total)

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