Customizing output

Introduction to Oracle SQL

Hadrien Lacroix

Content Developer

Functions

Functions can be used for:

  • calculation
  • formating
  • manipulation
  • conversion between data types
Introduction to Oracle SQL

Functions and data types

Numeric data Character data Date data
AVG x
SUM x
MIN x x x
COUNT x x x

Data types define what type of data a column can contain.

Introduction to Oracle SQL

Types of functions

  • Character functions
    • Input: character values
    • Outputs: character, numeric, date values
  • Number functions
    • Input: numeric values
    • Outputs: numeric values
  • Date functions
  • General functions
  • Conversion functions
Introduction to Oracle SQL

Case manipulation: upper case

UPPER(column): converts all alpha character values to uppercase

SELECT UPPER(State) AS State, UPPER(PostalCode) AS PostalCode
FROM Customer
| State | PostalCode |
|-------|------------|
| CA    | 95014      |
| DF    | 71020-677  |
| AB    | T6G 2C7    |
| BC    | V6C 1G8    |
| QC    | H2G 1A7    |
| ...   |            |
Introduction to Oracle SQL

Case manipulation: lowercase

LOWER(column): converts all alpha character values to lowercase

SELECT LOWER(Email) AS LowercaseEmail
FROM Customer
| LowercaseEmail           |
|--------------------------|
| [email protected]     |
| [email protected]    |
| [email protected]      |
| [email protected]    |
| ...                      |
Introduction to Oracle SQL

Getting a substring

SUBSTR(column, m, n): returns a portion of a string from position m, n characters long

SELECT Phone
FROM Customer
| Phone             |
|-------------------|
| +56 02 635 4444   |
| +91 0124 39883988 |
| +44 0131 315 3300 |

Goal: Get the country code of a telephone number without +

Introduction to Oracle SQL

Getting a substring

Goal: get the country code of a telephone number without +

SELECT Phone, SUBSTR(Phone, 2, 2) AS cc
FROM Customer
| Phone             | cc |
|-------------------|----|
| +56 (0)2 635 4444 | 56 |
| +91 0124 39883988 | 91 |
| +44 0131 315 3300 | 44 |
| +39 06 39733434   | 39 |
| +48 22 828 37 39  | 48 |
| ...               |    |
Introduction to Oracle SQL

Nested functions

Goal: Generate usernames for customers from first 5 letters of their last name and their id

SELECT LastName, CustomerId, CONCAT(SUBSTR(LastName,1,5), CustomerId) AS UserName
FROM customer
| LastName | CustomerId | UserName |
|----------|------------|----------|
| Almeida  | 12         | Almei12  |
| Barnett  | 28         | Barne28  |
| Bernard  | 39         | Berna39  |
| Brooks   | 18         | Brook18  |
| Brown    | 29         | Chase21  |
| ...      |            |          |
Introduction to Oracle SQL

Other useful character functions

LENGTH(val): returns length of a string

SELECT LENGTH('cat')
3

REPLACE(val, m, n): replace m with n in val

SELECT REPLACE('kayak', 'k', 'y')
yayay
Introduction to Oracle SQL

Rounding

ROUND(column, m): round column to m decimal

SELECT Total, ROUND(Total, 1) AS Round1, ROUND(Total, 0) AS Whole 
FROM Invoice
| Total     | Round1 | Whole |
|-----------|--------|-------|
| 11.94     | 11.9   | 12    |
| 14.91     | 14.9   | 15    |
| 0.99      | 1.0    | 1     |
| 5.94      | 5.9    | 6     |
| 7.96      | 8.0    | 8     |
| ...       |        |       |
Introduction to Oracle SQL

Truncating

TRUNC(column, m): truncates column to m decimal

SELECT DISTINCT Total, ROUND(Total, 1) AS Dec1, TRUNC(Total, 1) AS Trun1 
FROM Invoice
| Total | Dec1 | Trun1 |
|-------|------|-------|
| 15.86 | 15.9 | 15.8  |
| 13.86 | 13.9 | 13.8  |
| 8.94  | 8.9  | 8.9   |
| 1.99  | 2.0  | 1.9   |
| 7.96  | 8.0  | 7.9   |
Introduction to Oracle SQL

Modulo

MOD(column1, column2): returns remainder of division

SELECT MOD(14, 4)
2
Introduction to Oracle SQL

Modulo

MOD(column1, column2): returns remainder of division

SELECT MOD(14, 2)
0
SELECT MOD(15, 2)
1
Introduction to Oracle SQL

Modulo

Do we have an even amount of employees?

SELECT MOD(COUNT(Employee),1) 
FROM Employee
0

Yes.

Introduction to Oracle SQL

Let's practice!

Introduction to Oracle SQL

Preparing Video For Download...