A function defined in a relational database system. Most RDBS's include numerous built-in functions and also allow for the creation of additional custom functions.
Questions tagged [sql-function]
908 questions
982
votes
19 answers
Function vs. Stored Procedure in SQL Server
When should I use a function rather than a stored procedure in SQL, and vice versa? What is the purpose of each?

Tarik
- 79,711
- 83
- 236
- 349
827
votes
12 answers
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
How can I achieve the equivalents of SQL's IN and NOT IN?
I have a list with the required values. Here's the scenario:
df = pd.DataFrame({'country': ['US', 'UK', 'Germany', 'China']})
countries_to_keep = ['UK', 'China']
#…

LondonRob
- 73,083
- 37
- 144
- 201
219
votes
9 answers
How to check date of last change in stored procedure or function in SQL server
I need to check when function was changed last time. I know how to check creation date (it is in function properties window in SQL Server Management Studio).
I found that in SQL Server 2000 it wasn't possible to check modify date ( look at this…

Marek Kwiendacz
- 9,524
- 14
- 48
- 72
200
votes
6 answers
MySQL stored procedure vs function, which would I use when?
I'm looking at MySQL stored procedures and functions. What is the real difference?
They seem to be similar, but a function has more limitations.
I'm likely wrong, but it seems a stored procedure can do everything and more than a stored function can.…

Anonym
- 7,345
- 8
- 35
- 32
157
votes
6 answers
How to check if a function exists in a SQL database?
I need to find out if a function exists in a database, so that I can drop it and create it again. It should basically be something like the following code that I use for stored procedures:
IF EXISTS (
SELECT *
FROM dbo.sysobjects
…

Dr. Greenthumb
- 2,280
- 5
- 27
- 33
126
votes
3 answers
MySQL: Selecting multiple fields into multiple variables in a stored procedure
Can I SELECT multiple columns into multiple variables within the same select query in MySQL?
For example:
DECLARE iId INT(20);
DECLARE dCreate DATETIME;
SELECT Id INTO iId, dateCreated INTO dCreate
FROM products
WHERE pName=iName;
What is the…

aHunter
- 3,490
- 11
- 39
- 46
112
votes
4 answers
How to create a temporary function in PostgreSQL?
I have to execute a loop in database. This is only a one time requirement.
After executing the function, I am dropping the function now.
Is there any good approach for creating temporary / disposable functions?

Anand
- 1,287
- 3
- 11
- 15
91
votes
3 answers
Table-Valued Function(TVF) vs. View
What's the difference between table-valued functions and views? Is there something you can do with 1 that's hard or impossible to do with the other? Or does the difference lie in efficiency?

Haoest
- 13,610
- 29
- 89
- 105
52
votes
6 answers
SQL Query - Concatenating Results into One String
I have a sql function that includes this code:
DECLARE @CodeNameString varchar(100)
SELECT CodeName FROM AccountCodes ORDER BY Sort
I need to concatenate all results from the select query into CodeNameString.
Obviously a FOREACH loop in C# code…

Matthew Jones
- 25,644
- 17
- 102
- 155
52
votes
10 answers
Select Rows with id having even number
I am passing a simple query where I am searching for specific rows where OrderID is an even number
SELECT *
FROM Orders
WHERE mod(OrderID,2) = 0;
Error :
Syntax error (missing operator) in query expression 'mod(OrderID,2) = 0'.

solanki kaushik
- 521
- 1
- 4
- 5
38
votes
1 answer
pass parameter in table valued function using select statement
I have created a table valued return function which returns me a table . here is call of my function as follow
SELECT * FROM dbo.[StateFixedTaxesCalculation](3020,16,1,1006)
and its working OK for me , now i want to use this function call in a…

rahularyansharma
- 11,156
- 18
- 79
- 135
38
votes
12 answers
How to Replace Multiple Characters in SQL?
This is based on a similar question How to Replace Multiple Characters in Access SQL?
I wrote this since sql server 2005 seems to have a limit on replace() function to 19 replacements inside a where clause.
I have the following task: Need to…

kiev
- 2,040
- 9
- 32
- 54
37
votes
2 answers
SQL function return-type: TABLE vs SETOF records
What's the difference between a function that returns TABLE vs SETOF records, all else equal.
CREATE FUNCTION events_by_type_1(text) RETURNS TABLE(id bigint, name text) AS $$
SELECT id, name FROM events WHERE type = $1;
$$ LANGUAGE SQL…

ma11hew28
- 121,420
- 116
- 450
- 651
34
votes
4 answers
INNER JOIN with Table-Valued Function not working
I have a table valued function that returns a table. When I try to JOIN the table-valued function with another table I don't get any results, but when I copy the result of the function into an actual table and do the same join, then I get expected…

user2343837
- 1,005
- 5
- 20
- 31
30
votes
5 answers
Calling Scalar-valued Functions in SQL
I have migrated a database from oracle, and now have a few Scalar-valued Functions.
However, when I call them, I get an error saying:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.chk_mgr", or the name is…

Madam Zu Zu
- 6,437
- 19
- 83
- 129