SQL User Defined Function Within Select

SqlSql ServerSelectUser Defined-Functions

Sql Problem Overview


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 ...

Sql Solutions


Solution 1 - Sql

Yes, you can do almost that:

SELECT dbo.GetBusinessDays(a.opendate,a.closedate) as BusinessDays
FROM account a
WHERE...

Solution 2 - Sql

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')

Solution 3 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmadcolorView Question on Stackoverflow
Solution 1 - Sqluser17670View Answer on Stackoverflow
Solution 2 - SqljerryhungView Answer on Stackoverflow
Solution 3 - SqlrecursiveView Answer on Stackoverflow