73

I have a user defined function in SQL called getBuisnessDays it takes @startdate and @enddate and returns the number of business days between the two dates. How can I call that function within my select?

Here's what I'd like to do..

SELECT getBusinessDays(a.opendate,a.closedate) 
FROM account a
WHERE ...
1
  • 3
    Just remember that UDFs can often kill query performance depending on how the optimizer reacts to them. They often mean processing row by row rather than set-based. For your situation you might be better off using a calendar table and selecting against that when needed.
    – Tom H
    Dec 12, 2008 at 20:11

3 Answers 3

124

Yes, you can do almost that:

SELECT dbo.GetBusinessDays(a.opendate,a.closedate) as BusinessDays
FROM account a
WHERE...
1
  • 6
    damn - beat me to it - you need the owner prefix
    – DJ.
    Dec 12, 2008 at 19:25
14

If it's a table-value function (returns a table set) you simply join it as a Table

this function generates one column table with all the values from passed comma-separated list

SELECT * FROM dbo.udf_generate_inlist_to_table('1,2,3,4')
8

Use a scalar-valued UDF, not a table-value one, then you can use it in a SELECT as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.