From the course: Querying Microsoft SQL Server 2022
Use functions in T-SQL - SQL Server Tutorial
From the course: Querying Microsoft SQL Server 2022
Use functions in T-SQL
- [Instructor] SQL Server ships with a large number of built-in functions that you can leverage to manipulate the data that's stored in your database tables. In this chapter, I'm going to introduce you to some of my favorites, but I thought it'd be helpful to get an overview first. The built-in functions fall into a number of categories. We've already talked about the aggregate functions that are typically paired with the GROUP BY clause. These will help you find the sum total or count or average of a number of values within a group. String functions manipulate text strings by extracting and replacing characters or joining text together. Mathematical functions can be useful for rounding values, finding logarithms and exponents, or working with geometry, such as calculating sine, cosine, and tangents. There are also math... Rephrase. There are also mathematical functions for converting between radians and degrees or accurately referencing the constant value of pi for working with circles and arc geometry. Date and time functions will help you calculate the amount of time that's elapsed between two dates, or by adding time intervals together. Functions that extract portions of a date, for instance, getting the name of the month from a date field, also fall into the date and time function category. And there are logical functions which can be used to evaluate conditions and return values based on whether a condition is true or false. These allow you to easily build in some basic automation into your queries. That's useful for having the query evaluate and make decisions based on your data. To use a function, you simply type its name. The usual convention is to use all capital letters for function names in your scripts. Then inside of parentheses, you'll include arguments that the function requires to know as a comma-separated list. These arguments are the parameters that you use to customize the function's output. Some functions will have several arguments that you need to provide, and other functions are self-sufficient and don't require any additional arguments. So for instance, there is a string function called LEFT that'll return the specific number of characters from a beginning of a text string. In order to apply it to the data in the FirstName column of a table, you supply the name of the column as the first argument, and the number of characters that you want to pull out as the second argument. If you use this function to process the name Adam, it'll just return the letter A. Now, there are a lot of built-in functions that are included with SQL Server, and there's way more than I can cover in this course. Some of them are useful in everyday activities and some are highly specialized. I'd recommend bookmarking this URL so that you'll have an easy reference to all of the functions that you can use in SQL Server 2022. At the top, we have headers that describe the basic categories, so there's aggregate functions, analytic functions, and so on.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Use functions in T-SQL3m 10s
-
(Locked)
String functions3m 44s
-
(Locked)
Text concatenation3m 40s
-
(Locked)
Round with mathematical functions3m 13s
-
(Locked)
Find the GREATEST or LEAST value5m 5s
-
(Locked)
Work with date functions5m 56s
-
(Locked)
FORMAT date and times3m 36s
-
(Locked)
Group dates into buckets6m 10s
-
(Locked)
Return random records with NEWID2m 22s
-
(Locked)
Create a series of values5m 13s
-
(Locked)
The IIF logical function3m 39s
-
(Locked)
Write a CASE statement4m 34s
-
(Locked)
Challenge: Use functions in a SQL query49s
-
(Locked)
Solution: Use functions in a SQL query13m 55s
-
(Locked)
Solution: Use functions in a query4m 22s
-
-
-
-
-
-