Built-In Functions (SQL Server)
Built-In functions are used in SQL SELECT expressions to calculate values and manipulate data. These functions can be used anywhere expressions are allowed. Common uses of functions include to change a name to all upper case. In this article we’ll introduce you to basic concepts.
All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database. You can get started using these free tools using my Guide Getting Started Using SQL Server.
What are Built-In Functions?
In SQL a built-in function is a piece for programming that takes zero or more inputs and returns a value. An example of a built-in functions is ABS(), which when given a value calculates the absolute (non-negative) value of the number.
Some functions, such as ABS() are used to perform calculations, other such as GETDATE() are used to obtain a system value, such as the current data, or others, like LEFT(), are used to manipulate textual data.
Here is a simple query using the absolute value function.
SELECT Name, ABS(500 - ReorderPoint) ReorderPointDeviation FROM production.Product WHERE ABS(500 - ReorderPoint) > 200
In this query we first calculate the difference between 500 and a product’s reorder point. The ABS function is then used to return the result as a positive number.
There are several things to note regarding functions.
- The inputs to a function are called parameters. Not all function have parameters, and some functions have more than one.
- Parameters are enclosed in parenthesis.
- We use functions in the SELECT clause as well as WHERE filter condition. A function can be used anywhere in a SELECT statement that you can use an expression.
- Function are reserved words. I would avoid using them as column or table names. If you do, then expect to qualify your names with brackets .
As you learn more about functions you soon find they are vital in being able to calculate and manipulate your query results. We’ll dig deeper into specific functions and their uses in future posts, but before we do so, let’s talk about the type of data a function can return.
What Can Functions Return?
When most of us think about functions we think fondly of math class – ah those memories… :)
Most folk’s first impression is that functions are used to return numeric values.
Sure, they are used for these, but functions can return many other data types as well.
As you’ll see, functions are used to manipulate DATETIME, VARCHAR, and REAL types.
In SQL server the built-in functions return one value. These are called scalar functions, and are used wherever expressions are allowed.
Scalar is just a fancy work for “single value.” You will also learn about functions that can return table rows, these functions are called table value functions. You’ll use these later on when you create your own user defined functions.
How do Functions Behave?
Some functions return the same value each time you call them. These are said to be deterministic functions. For a given input, these functions return the same value each time they are called..
SQRT(), which is used to return the square root of a number, is deterministic. No matter how many times you run
It will return 3.
Non-deterministic functions’ return value may change from execution to execution. GETDATE(), which returns the current date and time, is a good example.
Returns a different value second by second.
This is important to keep in mind when writing and troubleshooting queries. If your query contains non-deterministic functions, then you should expect the results to change. If you didn’t think of this and you were comparing query results you may think the inconsistent results indicated a major problem, when it was only a non-deterministic function behaving as it should.
Later, as you start to program SQL, and write your own UDF’s (User Defined Functions), and work with indexes, you find that non deterministic function limit where you can use incorporate your UDF’s into the database.
There are over a hundred built-in functions in SQL server. To understand their breadth of application, I would recommend visiting the Built-In Functions (Transact SQL) page on the MDSN site. You find the functions are categorized into major categories. The categories we’ll cover next are:
- Conversion Functions – Convert data with CAST and CONVERT
- Logical Functions – execute one expression versus another depending on the outcome of a logical comparison.
- Math Functions – perform advanced calculations and round numbers.
- String Functions – change text values to all upper case, or remove the trailing spaces from values.
- Date Functions – add days or months to a date. Calculate the day of week from the date.