Questions tagged [user-defined-functions]

A function provided by the user of a program or an environment most often for spreadsheet type applications or database applications. Use [custom-functions-excel] for Excel and [custom-function] for Google sheets. Specify a programming language tag as well: [google-apps-script], [javascript], [sql], [tsql], etc. as well as a tag for the application: [excel], [google-spreadsheet], [sql-server] etc.

In the context of a programming language or an environment, a User Defined Function (UDF) is a function that is created by a user to perform a specific task (as opposed to a function that is intrinsic to the environment, built into the programming language or environment).

Spreadsheet applications like Excel and Google Sheets calls these "custom functions".

Microsoft also uses the term User Defined Functions with . The tag may also be applicable. See What is the need for user-defined functions in SQL Server?

Use:

4875 questions
244
votes
3 answers

How is "=default" different from "{}" for default constructor and destructor?

I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question: If I want to give my class a destructor that is virtual, but is otherwise the same as what…
218
votes
21 answers

How to strip all non-alphabetic characters from string in SQL Server?

How could you remove all characters that are not alphabetic from a string? What about non-alphanumeric? Does this have to be a custom function or are there also more generalizable solutions?
Even Mien
  • 44,393
  • 43
  • 115
  • 119
212
votes
9 answers

Multi-statement Table Valued Function vs Inline Table Valued Function

A few examples to show, just incase: Inline Table Valued CREATE FUNCTION MyNS.GetUnshippedOrders() RETURNS TABLE AS RETURN SELECT a.SaleId, a.CustomerID, b.Qty FROM Sales.Sales a INNER JOIN Sales.SaleDetail b ON a.SaleId = b.SaleId …
AndrewC
  • 6,680
  • 13
  • 43
  • 71
168
votes
12 answers

Overriding a JavaScript function while referencing the original

I have a function, a(), that I want to override, but also have the original a() be performed in an order depending on the context. For example, sometimes when I'm generating a page I'll want to override like this: function a() { new_code(); …
Kev
  • 15,899
  • 15
  • 79
  • 112
164
votes
9 answers

How to report an error from a SQL Server user-defined function

I'm writing a user-defined function in SQL Server 2008. I know that functions cannot raise errors in the usual way - if you try to include the RAISERROR statement SQL returns: Msg 443, Level 16, State 14, Procedure ..., Line ... Invalid use of a…
EMP
  • 59,148
  • 53
  • 164
  • 220
133
votes
2 answers

Declare variable in table valued function

How can I declare a variable in a table valued function?
esquare
  • 3,947
  • 10
  • 34
  • 37
131
votes
1 answer

Define all functions in one .R file, call them from another .R file. How, if possible?

How do I call functions defined in abc.R file in another file, say xyz.R? A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line?
G Shah
  • 2,045
  • 2
  • 16
  • 17
125
votes
6 answers

Adding code to a javascript function programmatically

I'm attempting to customize an existing JS library without modifying the original JS code. This code loads in a few external JS files which I do have access to, and what I'd like to do is change one of the functions contained in the original file…
Munzilla
  • 3,805
  • 5
  • 31
  • 35
115
votes
29 answers

How to determine the number of days in a month in SQL Server?

I need to determine the number of days in a month for a given date in SQL Server. Is there a built-in function? If not, what should I use as the user-defined function?
Even Mien
  • 44,393
  • 43
  • 115
  • 119
83
votes
4 answers

Spark functions vs UDF performance?

Spark now offers predefined functions that can be used in dataframes, and it seems they are highly optimized. My original question was going to be on which is faster, but I did some testing myself and found the spark functions to be about 10 times…
81
votes
8 answers

TSQL How do you output PRINT in a user defined function?

Basically I want to use PRINT statement inside a user defined function to aide my debugging. However I'm getting the following error; Invalid use of side-effecting or time-dependent operator in 'PRINT' within a function. Can this not be…
c00ke
  • 2,245
  • 5
  • 26
  • 34
80
votes
9 answers

Refresh Excel VBA Function Results

How can I get a user-defined function to re-evaluate itself based on changed data in the spreadsheet? I tried F9 and Shift+F9. The only thing that seems to work is editing the cell with the function call and then pressing Enter.
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
73
votes
3 answers

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…
madcolor
  • 8,105
  • 11
  • 51
  • 74
69
votes
5 answers

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.Splitfn", or the name is ambiguous

I've used the following split function: CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) …
ACP
  • 34,682
  • 100
  • 231
  • 371
64
votes
2 answers

Apache Spark -- Assign the result of UDF to multiple dataframe columns

I'm using pyspark, loading a large csv file into a dataframe with spark-csv, and as a pre-processing step I need to apply a variety of operations to the data available in one of the columns (that contains a json string). That will return X values,…
1
2 3
99 100