Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL User Defined Function Within Select

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 ... 
like image 769
madcolor Avatar asked Dec 12 '08 19:12

madcolor


People also ask

Can we call user-defined function in SELECT statement?

User-defined functions can't call a stored procedure, but can call an extended stored procedure. User-defined functions can't make use of dynamic SQL or temp tables. Table variables are allowed. SET statements aren't allowed in a user-defined function.

Can we use function in SELECT statement?

Functions can be used anywhere in SQL, like AVG, COUNT, SUM, MIN, DATE and so on with select statements. Functions compile every time. Functions must return a value or result. Functions only work with input parameters.

Can functions be called from within an SQL SELECT statement?

When calling a function from a SELECT statement, that function can only perform DML (insert, update, delete) statements if it is defined as an AUTONOMOUS_TRANSACTION. 2. You should avoid using RESULT_CACHE with temporary tables. The PL/SQL documentation even presents this as a restriction.


2 Answers

Yes, you can do almost that:

SELECT dbo.GetBusinessDays(a.opendate,a.closedate) as BusinessDays FROM account a WHERE... 
like image 102
user17670 Avatar answered Oct 02 '22 21:10

user17670


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') 
like image 21
jerryhung Avatar answered Oct 02 '22 19:10

jerryhung