Built in Functions
Built in Functions
Like most programming languages, SQL supports the use of functions to manipulate data.
Functions are operations that are usually performed on data to facilitate conversion and other
manipulation.
Think of SQL functions as tools designed to accomplish well defined tasks. For example, calculating
square root, or converting lowercase characters to uppercase. You invoke a function within SQL
query by name. Some functions take parameters, (also known as arguments) and some do not.
If a function requires parameters, they must be passed by placing them as a comma separated list
within parenthesis. Once the function performs its task, the function returns a value which is the
result of the execution of that function.
Unlike the base SQL statements, which for the most part are standard and are supported equally by
most DBMS. Most functions tend to be very DBMS vendor specific. In fact very few functions
are supported identically by all major vendors. Although all vendors provide functions that have
the same functionalities, vendor specific implementations of those functions tend to vary between
one vendor and another. .
Here is an example of some functions and vendor implementations:
Type of Functions
Numeric functions – Functions that work on numeric columns or values that are numbers.
String functions – Functions that work on textual/descriptive columns or values that are text.
Date functions – Functions that deal with date and time
Aggregate functions – Functions that allow you to sum, count average, etc.
REGEX functions – Functions that allow you to perform granular text searching/matching
I - Numeric Functions
Most DBMS provide functions to enable you to perform mathematical operations on numerical
data. These functions usually take one or more numeric parameters, and return a single numeric
value.
Note: To read the function list below, read the line that represents the DBMS you are interested in.
If there is no corresponding function on the same line, then the function is the same as the
line above..
Examples:
SELECT course_id, price, CEIL(price * 1.05)
FROM course
II - String Functions
Most DBMS provide many functions to enable you to work with, manipulate, and convert
character text strings. These functions are very flexible and powerful, and allow you to mold your
data to the specific look that you require. Some of these are…
Examples:
SELECT CONCAT(fname, ' ', lname) AS fullname /* MySql only */
FROM student
/* Oracle only */
SELECT fname || ' ' || lname AS fullname /* Oracle CONCAT( ) */
FROM student /* allows 2 fields only */
SELECT fname, lname, sex FROM instructor /* test the value */
WHERE LOWER(lname) = 'sultan' /* case insensitive */
SELECT description, LENGTH(description), course_id
FROM course
SELECT RTRIM(description), LPAD(price*1.03,10,'$') /* $$$1127.85 */
FROM course
SELECT course_id, SUBSTR(description, 1, 15)
FROM course
Nesting Functions
You can always nest one function within another. This allows you to build very sophisticated
capabilities to achieve the desired results. When nesting functions, the entire inner function
including all of its parameters will typically replace a single parameter of the outer function.
As you can see, you can develop very sophisticated capabilities by nesting one SQL function within
another function. There is no limit to the number of nested functions you can have. When nesting
functions, be careful to use the correct punctuation (parenthesis, commas, etc.) for each function.
The CASE Expression
CASE expression is often over-looked but can be extremely useful to change very complex query
requirements into simpler, and sometimes more efficient SQL statements.
The CASE expression enables many forms of conditional processing to be placed into a SQL
statement. By using CASE, more logic can be placed into SQL statements instead of being
expressed in a host language such as Java, Python, PHP, etc.
SELECT c.course_id ,
CASE s.sex
WHEN 'M' THEN concat(s.fname, s.lname)
ELSE null -- ELSE null is not needed
END as "Male" , -- but trying to make a point
CASE s.sex
WHEN 'F' THEN concat(s.fname, s.lname)
END as "Female" ,
sex
FROM class c, student s
WHERE c.stu_ssn = s.ssn
ELSE 'expensive'
END as "case results"
FROM course
Can I use a column alias on the SELECT or WHERE clause? (Self Reading)
You are always free to assign an alias to a column. This is often useful when the column is a
derived column which is a result of a function or case expression. The column alias will be used
as the column header for the resulting output.
In most databases, including Oracle and MySQL, you cannot reuse the alias on the SELECT or
in the WHERE clause. The aliased name is not known yet. As such, if you want to filter your
rows by the resulting value of the function or case expression, you must repeat the code of that
function or case expression in the SELECT or WHERE clause.
Examples:
SELECT description, price, price*1.05 AS new_price, ROUND(new_price, 2)
FROM course
I optionally placed the above CASE expression is parenthesis to help readability and code isolation.
Most DBMS provide functions to allow you to obtain the current date and time from the DBMS,
and to enable you to work with and perform various manipulations for date and time.
Most Databases use a different format for date & time, and also use different functions to
manipulate such dates and times. However, although most DBMS provide different date/time
functions, they all provide common functionalities for the manipulation of dates and times.
Date and time are stored using date datatypes, and each DBMS uses its own special format.
Date and time values are stored in special formats so that they can be sorted, filtered and
computed quickly and efficiently, and to save space internally.
The format used to store dates and times are usually of no consequence to you, and so date and time
functions are almost always used to read, extract, expand, and manipulate these dates and times.
These functions are crucial to your application if you need to deal with dates as most applications do.
Unfortunately, these functions tend to be least consistent between one DBMS and another.
Dates in MySQL:
Function Description
To obtain the time zone offset between the local time and the GMT time, simply subtract NOW( )
from UTC_TIMESTAMP( )
Examples:
Result: 13 47 17
Please note: If a function takes a date or time argument, a datetime type can also be given.
Dates in Oracle:
Function Description
+ and - You can use the plus and minus operators to add
and subtract dates.
(add and subtract) Values are always in full and fraction of days
TO_CHAR(date, 'format') Convert a date to a string using the any of
the following formats:
Example:
SELECT SYSDATE, TO_CHAR(sysdate, 'mm/dd/yy hh :mi:ss am') -- Display date.
FROM dual -- to display the time
-- you must use to_char( )
Result: 12-JUN-24 06/12/24 01:07:14 pm
From dual ?
Oracle has a dummy table called DUAL. This table is used whenever you need to a retrieve system
information. The table has a single column called “DUMMY” with a single row containing “X”.
The purpose of this table is to allow you to retrieve a single row, and to complete the SELECT
statement with the required FROM clause. Other databases may not require a FROM clause.
Examples:
SELECT SYSDATE, SESSIONTIMEZONE FROM dual
Result: 12-JUN-24 -04:00 -- today and time zone
SELECT sysdate + 20, sysdate - 6.5/24 -- Add 20 days, Subtract 6.5 hours
From dual
PostgreSQL
SQLite
MS SQL Server
IV - Aggregate Functions
We will discuss aggregate functions including the list below in a future session.
For the most part, most aggregate functions tend to be standard between implementations of
various DBMS.
Aggregate functions are often used in conjunction with the GROUP BY clause.
Function Description
SUM(column) Total the values for all (or some) of the instances for the
specified numeric column.
COUNT(column) Count the number of occurrences for all (or some) of the
COUNT(*) instances for the specified column.
COUNT(*) – count entire row
AVG(column) Compute the average of all (or some) the instances of the
specified numeric column.
MIN(column) Return the minimum value of the specified column.
Can also be used for text columns
MAX(column) Return the maximum value of the specified column.
Can also be used for text columns
MEDIAN(column) Return the median (mid-point value) of a numeric column.
- If number of rows is odd, you get the value for the row.
(Oracle only) - If number of rows is even, you get the average of the 2 rows.
GROUP_CONCAT(…) Concatenate all the values of a single (or multiple) columns.
Values are separated with a comma by default
(MySql and SQLite)
LISTAGG(…) Concatenate all the values of a single or multiple columns.
Values are not separated by default
(Oracle only)
Important:
Unlike all functions described previously, where the functions work horizontally within a single
individual row, all of the above aggregate functions work vertically across multiple rows.
Most new releases of database engines including both MySql and Oracle support the concept of
Regular Expression (also known as REGEX).
You can use a special regular expression language to construct a pattern which the column value
must match in order to be selected. The regex language is based on the Perl regular expression
language. It is a very sophisticated mechanism that allows you to create detail patterns that text
content or column values must match in order to be accepted.
As you recall, you can use the LIKE command to test whether a column value matches (or does not
match) a given pattern. The pattern is typically made up of any character, as well as 2 special
wildcard characters % and _
Using REGEX is similar to using the LIKE command, except that regex offers us a much wider and
broader set of wildcard and meta-characters, and allows us to define more complex patterns.
Using MySQL:
MySql implemented regex by providing a comparison operator called RLIKE, also called REGEXP.
You can use the operator exactly as you would use the LIKE operator.
Examples:
select * from course --or you can use REGEXP
where description RLIKE '^java' --starts with java
Using ORACLE:
Oracle implemented regex functionality by providing a few new regex functions, mainly:
REGEXP_LIKE(string, regex, mode) Allows you to find a regex pattern in a string value
Mode is 'i' for case insensitive. Default 'c' case sensitive.
REGEXP_REPLACE(string, regex, replacement, position, occurrence, mode)
Find a pattern, and replace what is found with the replacement string value
Position allows you to specify a starting position. Default is position 1.
Occurrence allows you to specify which occurrence to replace. Default is 0 (all).
Examples:
select * from course
where REGEXP_LIKE(description, '^java', 'i' ) --starts with java
REGEX in MySQL
MySql does not support the classes such as \s for any space, \d for digit, \w, \b, etc. Instead,
MySql offers the following meta-character classes as substitute:
REGEX in Oracle
select case
when REGEXP_LIKE('999-99-9999', '^\d\d\d-\d\d-\d{4}$')
then 'Valid' else 'Invalid' end as "regex test"
from dual
Function Description
Examples:
Function Description
To get AM/PM:
SELECT CASE WHEN strftime( '%H', 'now', 'localtime') < '12' THEN 'AM'
ELSE 'PM'
END as "AM/PM"
Examples:
CONVERT(to, from, style) Convert any value from one datatype to another.
Can be used with dates using the following styles:
Examples: