I want to know that how to create a scalar valued function in SQL Server. Please explain it with example.
Thank you
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
MuthuMari MRank #5,738 · 288 reputationPosted Apr 24, 2012, 9:31 PM EDT
A scalar-valued function (SVF) is a user-defined function (UDF) that returns a single value. Scalar-valued functions can take arguments and return values of any scalar data type supported by SQL Server except rowversion, text, ntext, image, timestamp, table, or cursor.
pls refer this url for samples
http://www.yaldex.com/sql_server/progsqlsvr-CHP-5-SECT-1.html
thanks.
If this post is useful then mark it as "Accepted Answer"
Pankajkumar PatelRank #181 · 13.4K reputationPosted Nov 16, 2021, 9:32 AM EST
Hi Vipendra Verma,
For how to create a scalar valued function in SQL Server. Please refer the below article with example:
Hope, this will help you!
Vinitha TRank #8,005 · 230 reputationPosted Nov 16, 2021, 9:16 AM EST
BEGIN
Nitesh KejriwalRank #253 · 9.9K reputationPosted Apr 24, 2012, 5:14 PM EDT
Introduced in SQL 2005, a scalar-valued function (SVF) is a function that returns a single value, such as a string, integer, or bit value. You can perform any operation in the function, but the final output will be a single value. In programming terms, it is a function that returns a base datatype(int/string/bool etc.)
For example-
CREATE FUNCTION GetRowsCount RETURNS int AS BEGIN DECLARE @RowsCount int SELECT @RowsCount = COUNT(*) AS 'Order Count' FROM SalesOrderHeader RETURN @RowsCount ENDSatyapriya NayakRank #47 · 31.5K reputationPosted Apr 24, 2012, 7:14 PM EDT
Please refer the below link
http://codingresource.blogspot.in/2010/01/creating-scalar-valued-functions-in-sql.html
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted Apr 24, 2012, 7:28 PM EDT
Functions are similar to functions you write in any programming languages. A function is a piece of code or routine that accepts parameters and is stored as an object in SQL server.
Function always return result set from a function
One of the key difference between functions and stored procedure is that function can be called within a SELECT statement or even in WHERE clause, while a stored procedure is called by using an EXECUTE procedure statement.
Scalar Function
A scalar function returns a single data value.
CREATE FUNCTION [scheme_name.]function_name
( [ @parameter_name AS [type_schema_name.]parameter_data_type
= default ],…n]
]
)
RETURNS return_data_type
[WITH
AS
BEGIN
RETURN scalar_expression
END [ ; ]
Example:
Create Function dbo.fn_dollar_toEuro(@dollar money)
returns money
as
begin
declare @result money
set @result = @dollar /1.10
return @result
end
print dbo.fn_dollar_toEuro(10)
Create FUNCTION [dbo].[Get_Active_Rule_Status]
(
@ReviewName nvarchar(50),
@RuleName nvarchar(50)
)
RETURNS BIT
AS
BEGIN
RETURN (SELECT cast(apply_rule as bit) from PVH_Rules where ReviewName = @ReviewName and RuleName = @RuleName)
END
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 24, 2012, 7:38 PM EDT
A Scalar user-defined function returns one of the scalar data types. Text, ntext, image and timestamp data types are not supported.
You pass in 0 to many parameters and you get a return value.
Example
CREATE FUNCTION MyValue
(@value int)
RETURNS varchar(30)
AS
BEGIN
declare @Return varchar(30)
select @return = case @value
when 1 then 'One'
when 2 then 'Two'
when 3 then 'Three'
when 4 then 'Four'
when 5 then 'Five'
else 'More than Five'
end
return @return
Also think about CTE.end
hope this help.
geetha geethaRank #7,130 · 248 reputationPosted Nov 23, 2021, 7:57 AM EST
Yazhini MP128 reputationPosted Dec 3, 2021, 4:47 AM EST
Naresh BeniwalRank #365 · 6.8K reputationPosted Nov 22, 2022, 5:56 AM EST
https://www.javatpoint.com/scalar-functions-in-sql
Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 23, 2022, 9:26 AM EST
Basically, Scalar Value means which returns a single primitive value, For eg., you want to get the age of the person so you will pass the dob of the person which returns the age of the person.
please find the example for the same
CREATE FUNCTION getPersonAge(
@dob datetime
)
RETURN INT
AS
BEGIN
//Calculate the Current Date - @dob and return the age
END;