Advertisement
Advertisement


SQL User Defined Function Within Select


Question

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 ...
2015/03/18
1
57
3/18/2015 3:10:06 PM

Accepted Answer

Yes, you can do almost that:

SELECT dbo.GetBusinessDays(a.opendate,a.closedate) as BusinessDays
FROM account a
WHERE...
2015/03/19
99
3/19/2015 9:55:57 PM

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')
2016/01/29

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

2008/12/12

Source: https://stackoverflow.com/questions/363838
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]