Built-In Functions (SQL Server)

·

·

You can use Built-In functions in SQL SELECT expressions to calculate values and manipulate data. Use these SQL functions anywhere expressions are allowed.  Common uses of functions include changing a name to all upper case.  In this article, we’ll introduce you to basic concepts to use SQL Function in SQL Server, MySQL, PostgreSQL, and Oracle.

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  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 function 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, others 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.

  1. The inputs to a function are called parameters.  Not all function has parameters, and some functions have more than one.
  2. Parameters are enclosed in parenthesis.
  3. We use functions in the SELECT clause as well as the WHERE filter condition. A function can be used anywhere in a SELECT statement that you can use an expression.
  4. Functions 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…  :)

built-in functions in SQL Server are different than those we learn in math.
Learning Math

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 word 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

SELECT SQRT(9)

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.

SELECT GETDATE()

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 a nondeterministic function limit where you can use incorporate your UDF’s into the database.

Function Categories

There are over a hundred built-in functions in SQL Server.  To understand their breadth of application, I would recommend visiting the Here on the MDSN site.  The categories we’ll cover next are:

See Also:

5 responses to “Built-In Functions (SQL Server)”
  1. Great content in this blog, Currently I am exploring SQL Server, your blog helping me a lot, thanks

  2. Barak810

    Is there any way to view the definition of the built-in functions?
    Thanks.

    1. Kris Wenzel

      Hi,

      You can get the parameters, at least hints for them, when using Azure Data Studio, but I don’t know of a way to get to the code. That would be cool though! It would interesting to know if some Built-in functions call other built-in functions and what the native prog. language is.

  3. ujjwal

    i find sql query very difficult can you teach me easy way to learn those..

    1. Hi ujjwal! My Getting Started Page is a great place to start learning SQL. I would recommend taking a look there and enrolling in my free email course. It will help and also get you on the right track.

      Kris.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from the blog


MySQL PostgreSQL SQLite SQL Server