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
63
votes
5 answers

Execute Stored Procedure from a Function

I know this has been asked to death, and I know why SQL Server doesn't let you do it. But is there any workaround for this, other than using Extended Stored Procedures? And please don't tell me to convert my function into a procedure... So what I'm…
61
votes
10 answers

Pass table as parameter into sql server UDF

I'd like to pass a table as a parameter into a scaler UDF. I'd also prefer to restrict the parameter to tables with only one column. (optional) Is this possible? EDIT I don't want to pass a table name, I'd like to pass the table of data (as a…
Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
60
votes
4 answers

SQL Server 2008 - How do i return a User-Defined Table Type from a Table-Valued Function?

Here's my user-defined table type... CREATE TYPE [dbo].[FooType] AS TABLE( [Bar] [INT], ) This is what ive had to do in my table-valued function to return the type: CREATE FUNCTION [dbo].[GetFoos] RETURN @FooTypes TABLE ([Bar] [INT]) INSERT INTO…
59
votes
6 answers

Spark Error:expected zero arguments for construction of ClassDict (for numpy.core.multiarray._reconstruct)

I have a dataframe in Spark in which one of the columns contains an array.Now,I have written a separate UDF which converts the array to another array with distinct values in it only. See example below: Ex: [24,23,27,23] should get converted to [24,…
57
votes
5 answers

Return multiple values from a SQL Server function

How would I return multiple values (say, a number and a string) from a user-defined function in SQL Server?
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
57
votes
3 answers

Hive: Convert String to Integer

I am looking for a Built-in UDF to convert values of a string column to integer in my hive table for sorting using SELECT and ORDER BY. I searched in the Language Manual, but no use. Any other suggestions also welcome.
Srinivas
  • 2,479
  • 8
  • 47
  • 69
53
votes
5 answers

Derive multiple columns from a single column in a Spark DataFrame

I have a DF with a huge parseable metadata as a single string column in a Dataframe, lets call it DFA, with ColmnA. I would like to break this column, ColmnA into multiple columns thru a function, ClassXYZ = Func1(ColmnA). This function returns a…
50
votes
4 answers

Applying UDFs on GroupedData in PySpark (with functioning python example)

I have this python code that runs locally in a pandas dataframe: df_result = pd.DataFrame(df .groupby('A') .apply(lambda x: myFunction(zip(x.B, x.C), x.name)) I would like to run this in PySpark,…
47
votes
1 answer

How can I create a user-defined function in SQLite?

I am developing an application in android and I need to create an udf in sqlite. Is it possible to create it in sqlite? And if yes how to do this?
Mustafa Güven
  • 15,526
  • 11
  • 63
  • 83
46
votes
2 answers

SQL Server: Could not find type in the assembly

Assume the assembly dll: using Microsoft.SqlServer.Server; using System.Data.SqlClient; using System.Data.SqlTypes; using System; using System.Text; namespace CLRFunctions { public class T { [SqlFunction(DataAccess =…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
46
votes
5 answers

How to add description to functions and function parameters?

I'm writing a VB.NET function with a ton of overloads. I've seen that most .NET functions have parameter descriptions in IntelliSense. For example, when typing in String.Compare(, IntelliSense says Compares two specified System.String objects and…
Phonon
  • 12,549
  • 13
  • 64
  • 114
43
votes
9 answers

COLLECT_SET() in Hive, keep duplicates?

Is there a way to keep the duplicates in a collected set in Hive, or simulate the sort of aggregate collection that Hive provides using some other method? I want to aggregate all of the items in a column that have the same key into an array, with…
batman
  • 1,447
  • 5
  • 16
  • 27
42
votes
2 answers

How to define and use a User-Defined Aggregate Function in Spark SQL?

I know how to write a UDF in Spark SQL: def belowThreshold(power: Int): Boolean = { return power < -40 } sqlContext.udf.register("belowThreshold", belowThreshold _) Can I do something similar to define an aggregate function? How is…
39
votes
7 answers

Deterministic function in mysql

I got confused with a seemingly simple concept. Mysql defines deterministic function as a function that always produces the same result for the same input parameters So in my understanding, functions like CREATE FUNCTION foo (val INT) READS SQL…
a1ex07
  • 36,826
  • 12
  • 90
  • 103
39
votes
4 answers

How to call a MySQL stored procedure from within PHP code?

I have stored procedure that I created in MySQL and want PHP to call that stored procedure. What is the best way to do this? -MySQL client version: 4.1.11 -MySQL Server version: 5.0.45 Here is my stored procedure: DELIMITER $$ DROP FUNCTION IF…
Pheap
  • 2,349
  • 4
  • 18
  • 13