Chapter 11-1 SQL Functions - Character
Chapter 11-1 SQL Functions - Character
Chapter 11
SELECT Statement
Without a Database Table
• Oracle and DB2 support techniques used to execute a SELECT
statement that does not access a table
• SQL Server and MySQL do not require a FROM clause
2
SELECT Statement
Without a Database Table
DB2
SELECT CURRENT_DATE
FROM SYSIBM.SYSDUMMY1;
VALUES CURRENT_DATE;
4
DB2
SELECT (319/29) + 12
FROM SYSIBM.SYSDUMMY1;
5
DB2 – Alias
6
SQL Functions
• Predefined block of code that accepts arguments (parameters)
and returns output
• Used to manipulate data values
• Can be used in the SELECT, WHERE, and ORDER BY clauses
7
Types of Functions
• Two types of functions:
– Scalar or Single-row function
– Aggregate or Group or Multiple-Row function
8
Scalar or Single-Row Function
• Manipulates input values and returns a single value each
time it is invoked
• Often referred to as a single-row function because it
operates on data one row at a time and returns a single
value as each row is processed
• If a single-row function is applied to 12 rows, 12 results are
returned.
9
Scalar or Single-Row Function
10
Aggregate or Group or Multiple-Row Function
• Performs a calculation on data from multiple-rows that are
grouped together and returns a single value for each group
• Often referred to as a multiple-row function because it operates
on data from multiple-rows at a time and returns a single value
for each group.
• Discussed in chapter 12
11
Aggregate or Group or Multiple-Row Function
12
Single-Row Character Functions
• Two categories:
– Case-Manipulation Functions
Convert the case of character strings
– Character-Manipulation Functions
Manipulate character strings - join, extract, show,
find, pad, and trim
13
Case Conversion Functions
• LOWER
• UPPER
• INITCAP (Oracle only)
14
LOWER Function
• Converts an alpha character string to lowercase letters
• When used in a SELECT clause, the appearance of the data in the
results set is altered
• When used in a WHERE clause, the value in the comparison is
altered
15
LOWER Function
SELECT Clause – appearance is altered
16
LOWER Function
WHERE Clause – value is altered
17
LOWER Function
WHERE Clause and Host Variable
18
UPPER Function
• Converts an alpha character string to uppercase letters
• When used in a SELECT clause, the appearance of the data in the
results set is altered
• When used in a WHERE clause, the value in the comparison is
altered
19
UPPER Function
20
Character String Manipulation Functions
• Used to extract, change, format, or alter a character string
• Character String Manipulation Functions:
– Accept one or more character strings as arguments (parameters)
– Performs the required task on the input character strings
– Returns the altered value
21
CONCAT Function
• || (pipes) – ANSI Standard
• CONCAT Function:
– Joins two values together
Accepts two character strings as arguments
Some implementations allow for more than two string arguments
Joins the strings
Outputs one string
22
|| and CONCAT Functions
23
SUBSTR Function
substr(string, position)
substr(string, position, length)
• Extracts a substring from a character string argument
• Accepts three arguments (string, position, length)
– String is the character string from which the substring is to be
extracted
– Position is the starting position where the substring begins
– Length is the length of the substring to be returned (optional)
24
SUBSTR Function
substr(string, position, length)
• Position argument:
– Starting position
– If position is 0, it defaults to 1
• Length argument
– Length of string to be extracted
– Optional, and if omitted, returns all characters to the end of the string
25
SUBSTR Function
26
INSTR (In String) Function
• Returns the position of the first occurrence of a substring within
a character string
• If the substring is not found, zero is returned
27
INSTR Function
INSTR( source_string, substring [, start_position [, occurrance ] ] )
• source_string - the string to be searched
• substring - the character string to be searched for inside of source_string
• start_position - an optional argument. It is an integer value that tells where to
start searching in the source_string. If the start_position is negative, then it
counts back that number of characters from the end of the source_string and
then searches backwards from that position. If omitted, this defaults to 1
• occurrence - an integer indicating which occurrence of substring should be
search for. That is, should INSTR return the first matching substring, the second
matching substring, etc. This argument is optional. If omitted, it defaults to 1
• If the substring is not found in source_string, the INSTR function returns 0.
28
INSTR Function
INSTR( source_string, substring [, start_position [, occurrance ] ] )
INSTR('abcdabcd','b'); (returns 2)
INSTR('bacdabcb','b',1); (returns 1)
INSTR('abcdabcd','b',1); (returns 2)
INSTR('abcdabcd','b',4); (returns 6)
INSTR('abcdabcd','b',0); (returns 0 or NULL)
INSTR('abcdabcd','b',-1); (returns 6)
INSTR('abbdabcd','b',-4); (returns 3)
INSTR('abcdabcd','z'); (returns 0) – substring not found
• If start_position is 0, 0 or NULL is returned
29
INSTR Function
INSTR( source_string, substring [, start_position [, occurrance ] ] )
INSTR('abcdabcd','b',1,1); (returns 2)
INSTR('abcdabcd','b',1,2); (returns 6)
INSTR('abcdabcd','b',1,0);
Oracle - (returns: ORA-01428: argument '0' is out of range)
DB2 – returns NULL
INSTR('abcdabcd','b',1,-1);
Oracle - (returns: ORA-01428: argument '-1' is out of range)
DB2 – returns NULL
INSTR('abcdabcd','b',-1,1); (returns 6)
INSTR('abcbbbcd','b',-1,2); (returns 5)
INSTR('abcdabcb','b',-1,1); (returns 8)
30
LENGTH Function
• Receives a character string as an argument and returns the
number of characters (length) in that character string
31
TRIM Functions
Three trim functions that remove a specific character string from a
string:
• LTRIM function
• RTRIM function
• TRIM function
32
LTRIM Function
• LTRIM( string [, trim_string] )
33
LTRIM Function
• LTRIM( string [, trim_string] )
34
LTRIM Function
• LTRIM( string [, trim_string] )
35
LTRIM Function
36
RTRIM Function
RTRIM( string [, trim_string] )
37
RTRIM Function
• RTRIM( string [, trim_string] )
38
TRIM Function
• Removes all specified characters from either the beginning, the
end, or both beginning and end of a string
• Optional parameters indicate whether leading, or trailing, or
both leading and trailing pad characters should be removed
• Returns a value with data type VARCHAR
39
TRIM Function
40
LPAD and RPAD Functions
There are two functions used for padding:
• LPAD
• RPAD
41
LPAD Function
• Pads (adds characters to) the left-side of a character string,
resulting in a right-justified value
42
LPAD Function
LPAD(string, padding_length, [ padding_string ])
• string - the character string being modified. The padded characters
are added to the left-side of the string
• padding_length - the number of characters to return (not the
number of characters to add). If the padding_length is smaller than
the original string, the LPAD function truncates the string to the size
of the padding_length
• padding_string - optional and is the character string that is padded to
the left- side of the string. If this parameter is omitted, the LPAD
function will default to padding spaces to the left side of the43
string.
LPAD Function
44
LPAD Function
45
RPAD Function
• Pads or adds characters to the right-side of a character string,
resulting in a left-justified value.
46
LPAD Function
RPAD (input_string, length, padding_character)
47
RPAD Function
48
REPLACE Function
• The REPLACE function replaces all occurrences of a substring
within a string with a new substring
• Changes one pattern with another
49
REPLACE Function
REPLACE (character_string, string_to_replace, [replacement_string]
50
REPLACE Function
51
REPLACE Function
52
REPLACE Function
53
REPLACE Function
54
TRANSLATE
Changes a character string character by character
55
TRANSLATE Function
56
TRANSLATE Function
57
Nesting Functions and
Column Aliases with Functions
58
Host Variables
• Called Bind variables in Oracle world
• Replace the hardcoded value in your statement with
a :named_variable
• Starts with colon (:)
• DBMS will prompt for a value when statement is executed
• Alpha and date host variables are treated as character strings
and require single quotation marks
59
Host Variables
• Original query:
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE department_id = 10; (and then 90, 110 . . .)
60
Host Variables
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE department_id = :dept_id;
61
62