As in most programming languages, SQL has functions for processing data. It is worth noting that, unlike SQL statements, functions are not standardized for all types of DBMS, that is, to perform the same operations on data, different DBMSs have their own function names. This means that the query code written in one DBMS may not work in another, and this should be taken into account in the future. This mostly applies to functions for processing text values, converting data types and manipulating dates.
Most DBMSs support a standard set of function types, namely:
The SQL implementation in MS Access has the following functions for text processing:
|Selects the characters in the text from the left
|Selects the characters in the text on the right
|Selects characters from the middle of the text
|Converts characters to upper case
|Converts characters to lower case
|Removes all blank characters to the left of the text
|Removes all blank characters to the right of the text
|Removes all blank characters from both sides of the text
Let's convert product names to uppercase using the UCase() function:
SELECT Product, UCase(Product) AS Product_UCase
Let's separate the first three characters in the text using the LEFT() function:
SELECT Product, LEFT(Product, 3) AS Product_LEFT
Number processing functions are designed to perform mathematical operations on numerical data. These functions are designed for algebraic and geometric calculations, so they are used much less often than date and time processing functions. However, numeric functions are the most standardized across all versions of SQL. Let's look at the list of numerical functions:
|Returns the square root of the specified number
|Returns the absolute value of a number
|Returns the exponent of the specified number
|Returns the sine of the specified angle
|Returns the cosine of the specified angle
|Returns the tangent of the specified angle
We have listed only a few of the main features, but you can always refer to your DBMS documentation for a full list of supported features with detailed descriptions.
For example, let's write a query to obtain the square root of the numbers in the Amount column using the SQR() function:
SELECT Amount, SQR(Amount) AS Amount_SQR
Date and time manipulation functions are among the most important and frequently used SQL functions. In databases, date and time values are stored in a special format, so they cannot be used directly without additional processing. Each DBMS has its own set of functions for processing dates, which, unfortunately, does not allow them to be transferred to other platforms and SQL implementations.
List of some functions for handling date and time in MS Access:
|Returns part of a date: year, quarter, month, week, day, hour, minute, second
|Returns the year and month respectively
|Hour(), Minute(), Second()
|Returns the hour, minutes, and seconds of the specified date
|Returns the name of the day of the week
Let's look at an example of how the DatePart() function works:
SELECT Date1, DatePart("m", Date1) AS Month1
The DatePart() function has an additional parameter that allows us to display the required part of the date. In the example, we used the value of the parameter "m", which displays the month number (in the same way, we can display the year - "yyyy", the quarter - "q", the day - "d", the week - "w", the hour - "h" , minutes - "n", seconds - "s", etc.).
Statistical functions help us get ready data without sampling it. SQL queries with these functions are often used to analyze and generate various reports. An example of such selections can be: determining the number of rows in a table, obtaining the sum of values for a certain field, searching for the largest/smallest or average value in the specified column of the table. We also note that statistical functions are supported by all DBMS without any special changes in writing.
List of statistical functions in DBMS Access:
|Returns the number of rows in a table or column
|Returns the sum of the values in a column
|Returns the smallest value in a column
|Returns the largest value in a column
|Returns the average value in a column
Returns the number of all rows in the table:
SELECT COUNT(*) AS Count1
Returns the number of all non-empty rows in the Product field:
SELECT COUNT(Product) AS Count2
We intentionally removed one value in the Product column to show the difference in performance between the two queries.
SELECT SUM(Quantity) AS Sum1
WHERE Month = 'April'
With this request, we displayed the total amount of goods sold in the month of April.
SELECT SUM(Quantity*Amount) AS Sum2
As you can see, in statistical functions we can also perform calculations on several columns using standard mathematical operators.
SELECT MIN(Amount) AS Min1
SELECT MAX(Amount) AS Max1
SELECT AVG(Amount) AS Avg1