SQL Server Functions
SQL server functions are the sets of instructions that are executed to perform a specific task. These functions are the same as a mathematical function, which is used to execute a task with saving time and effort.
There are some specific task and executions which takes time with more computational power; these functions are used in such cases to make it easier, with lower rime complexities. One can enroll for the MySQL certification to master the functionalities.
- SQL functions are the set of instructions to perform a particular task with less time and effort needed.
- There are many types of SQL functions that are used for many purposes, like numerical, strings, date, and others.
- Scalar-valued SQL functions are the type of SQL function that returns a scalar value as an output.
- Table values SQL functions return a table of the data instead of returning scalar values.
SQL Server Functions
There are some in-built functions in the SQL Server. String, numerical, date, and conversions are one of the most used and popular built functions being used.
- This function returns the ASCII for the specific character in a string.
SELECT ASCII(Names) AS CodeForFirstChar FROM Names;
- These functions return the character depending upon the ASCII codes.
SELECT CHAR(33) AS CodeChar;
- This SQL function returns the characters from a string starting from the left.
SELECT LEFT('SQL', 3) AS String;
- This SQL function returns the characters from the right side of the string.
SELECT RIGHT('SQL', 2) AS String
- This SQL Function takes a string as input and returns the reversed version of the string.
- This SQL function returns the absolute value of the value passed in a function as input.
SELECT Abs(-22.5) AS Abs;
- This SQL function returns the average of the values passed in it.
SELECT AVG(Price) AS Average FROM Products;
- This SQL function returns the number of observations in the data column.
SELECT COUNT(ID) AS NUM FROM Products;
- This SQL function returns the logarithmic value of the input passed in it.
- This SQL function returns the square value of the input.
- This SQL function returns the current date and time.
- This SQL function returns the current month of the date.
SELECT MONTH(2022/11/21) AS Month;
- This SQL function returns the year of the date passed.
SELECT YEAR(2022/11/21) AS Year;
- This SQL function returns the current date of the date passed.
SELECT DAY(2022/11/21) AS Day;
- This SQL function returns the date as per the database date and time.
Some Advance Functions
- This SQL function is used to convert a datatype into a specified datatype mentioned.
SELECT CAST(255.5 AS int);
- This SQL function is used to return a specified value that is not null.
SELECT ISNULL(NULL, 'Something');
- This SQL function is used to return a current user name.
Types of SQL Functions
- When you are working with programming problems, the DRY principle must be followed, which basically means DON’T REPEAT YOURSELF.
- In SQL, there are functions developed like mathematics, using which the time, space, and efforts must be saved, and the same task can be executed with the desired output.
- Mainly, there are two types of SQL functions are used, table-valued and scalar-valued functions.
- The table-valued SQL functions are the type of SQL functions that basically returns the table as an output.
- The scalar value SQL functions are those function that returns scalar values as output like int, varchar, or float.
Scalar Valued SQL Functions
To create a scalar-valued SQL function, we can use CREATE FUNCTION command to create a function.
CREATE FUNCTION sales.NetSale( @numbers INT, @price DEC(10,2), @discount DEC(4,2) ) RETURNS DEC(10,2) AS BEGIN RETURN @numbers * @price * (1 - @discount); END;
Here in the above code, we can see that we created a SQL function called NetSale, in which we defined 3 parameter numbers, price, and discount. This function takes these 3 values as input and returns an output which is a multiplication of numbers and price and a discount substituted from 1.
SELECT sales.NetSale(10,100,0.1) net_sale;
In the above code, we can see that by using SELECT, we are calling a function that we created to pass the values of numbers, prices, and discounts. This function will multiply the values of numbers and prices and the value got from substituting a discount from 1.
The table-valued function is the type of SQL function which returns a table as an output. The results from these functions are in the form of a table.
Here to create a table-valued function, we can use CREATE FUNCTION command.
CREATE FUNCTION ProductInYear ( @year INT ) RETURNS TABLE AS RETURN SELECT name, year, price FROM production.products WHERE Year = @year;
In the above code, we can see that we are creating a function called ProductInYear, which will have a model_year parameter and return the values for a particular year.
This function will take a numerical value as a year and check if the same input value is present in the dataset. Let’s suppose we pass 2017 as a year, then the function will check if any data for 2017 is present in model_year and returns all the values matching the 2017 year.
SELECT * FROM ProductInYear(2020);
Output: Returns the table of products with the year 2020.
In this article, we discussed various SQL functions that can be applied to numerical and categorical data that, included string, numeric, date, and some advanced SQL functions. Further, then we discussed the table-valued and scalar-valued SQL functions with their working mechanisms, SQL query, and logic behind them. Knowledge about these SQL functions and their core intuitions will help one to understand the functions conceptually and help answer interview questions related to them as well.
Frequently Asked Questions(FAQs)
Q1. Define SQL Server Functions.
Answer: SQL Server functions are a set of statements in SQL that are used to take up certain tasks. These functions are similar to the mathematics functions that are basically used to perform common tasks.
Q2. Differentiate between scalar-valued and true-valued functions.
Answer: The method of implementation is the major difference between scalar-valued and true-valued functions. The scalar-valued function returns a single value, and the true-valued function returns the output in the form of a table as per the selected criteria.
Q3. Write about the rules for creating SQL server functions.
Answer: The rules followed are mentioned below,
- Function name should not start with a special character.
- These are operated only with the SELECT statement.
- TRY and CATCH statements should not be used with the functions.
- These functions use the input parameters only.
- The functions must and should return a value when implemented.
Q4. What are the types of user-defined functions in SQL servers?
Answer: SQL server function have two main categories of user-defined functions. Scalar functions that return a single value as output. Table-valued functions that return the output in the form of a table.
In this article, you learned about SQL Server Functions, different types, and some inbuilt functions with syntax. To learn more about the topic you can refer to these articles,