• Your function takes three parameters, yet in your query, you pass it four. Do you not get an error when you run the query?

    We may not be able to help you with performance without DDL and sample data, but if you could post an execution plan, we may be able to spot something obvious.

    John