SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL Functions

Text copied!

« Previous
Next »

SQL Functions

May 29, 2023, 11:11 a.m. under SQL

  • Hi! You should check NOT BETWEEN operator post first.

    In SQL, Function can be defined as a block/set of SQL statements that is used to perform a specific task or accomplish a particular purpose.

    It can be reused through multiple projects which reduces code redundancy. 'Code redundancy' refers to the practice of writing the same code multiple times within a project.

    Types of SQL functions :

    1. User-defined function

    2. System-defined function

    SQL Functions
    1. User-defined function :

    A user-defined function is a custom function created by a user. SQL Server allows you to create your own functions using the 'CREATE FUNCTION' statement that enables you to perform specific calculations.

    2. System-defined function :

    SQL Server provides extensive collection of built-in system functions is available that perform various tasks related to the database system. It is also famous as 'built-in functions'.

    Here's the syntax :
    CREATE FUNCTION function_name
    (
        @parameter1 data_type,
        @parameter2 data_type,
        ...
    )
    RETURNS output_data_type
    AS
    BEGIN
    
       -- Write SQL statements here to perform desired operation
    
        RETURN output_value;
    END;
            

    • Specify the function name after the 'CREATE FUNCTION' keyword.

    • Specify the parameter name using the "@" prefix and its data type within the parentheses ( ).

    • Specify the return data type after the 'RETURNS' keyword. It is the output data type.

    • 'AS BEGIN' keyword indicates that the function body has started.

    • 'END' keyword indicates that the function body has ended.

    • Specify the value after the 'RETURN' keyword. It is the output value that function will return.

    Input Parameter :

    In SQL Server, function accepts many or no parameter to execute specific calculation and returns a single result. A parameter is a value that is passed into a function, on which a specific task is performed.

    Return type :

    In SQL Server, every function must return a value. This means that if the output of the function is a number, you should define the return type as INT.

    Calling function :

    Calling function simply means executing or running it. To call a function in SQL Server, you can use the 'SELECT' statement in a SQL query.

    Here's the syntax :

    SELECT schema_name.function_name( arguments );
            

    • Specify the schema name and function name after the 'SELECT' keyword.

    • Specify the argument in 'parenthesis ( )'. An argument is a value that is passed into a function.

    Here's an example of SQL user-defined function :

    [i] Let's assume you want to create a function that adds two numbers.

    [ii] Run below SQL statement :

    CREATE FUNCTION AddtwoNumbers
    (
        @num1 INT,
        @num2 INT
    )
    RETURNS INT
    AS
    BEGIN
        RETURN @num1 + @num2;
    END;
            
    SQL Functions

    [iii] Above statement will create a new function named as 'AddtwoNumbers'.

    [iv] Next, let's execute this function. Run below SQL statement :

    SELECT dbo.AddtwoNumbers(1, 4);
            
    SQL Functions

    [v] Above statement adds values '1' and '4', hence value '5' is returned as output. Example : 1 + 4 = 5

    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What are the functions of SQL?

    Functions of SQL include data retrieval, manipulation, insertion, deletion, and management of databases.

    What are the 5 built-in functions in SQL?

    Five built-in functions in SQL are COUNT, SUM, AVG, MAX, and MIN.

    What are the two functions of SQL?

    The two primary functions of SQL are data manipulation language (DML) and data definition language (DDL).

    What is SQL method?

    SQL method refers to the process or technique used to execute SQL commands or queries to interact with databases.
    Thank You! You should check SQL built-in functions post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy