Uitvoer aanpassen

Introductie tot Oracle SQL

Hadrien Lacroix

Content Developer

Functies

Functies kun je gebruiken voor:

  • berekening
  • opmaak
  • bewerking
  • conversie tussen datatypen
Introductie tot Oracle SQL

Functies en datatypen

Numerieke data Tekstdata Datedata
AVG x
SUM x
MIN x x x
COUNT x x x

Datatypen bepalen welk soort data een kolom kan bevatten.

Introductie tot Oracle SQL

Typen functies

  • Tekenfuncties
    • Invoer: tekstwaarden
    • Uitvoer: tekst-, numerieke, datumwaarden
  • Getalfuncties
    • Invoer: numerieke waarden
    • Uitvoer: numerieke waarden
  • Datumfuncties
  • Algemene functies
  • Conversiefuncties
Introductie tot Oracle SQL

Hoofdletters wijzigen: hoofdletters

UPPER(column): zet alle letters om naar hoofdletters

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    |
| ...   |            |
Introductie tot Oracle SQL

Hoofdletters wijzigen: kleine letters

LOWER(column): zet alle letters om naar kleine letters

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

Een deelstring ophalen

SUBSTR(column, m, n): geeft een deelstring vanaf positie m van n tekens lang

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

Doel: haal de landcode uit een telefoonnummer zonder +

Introductie tot Oracle SQL

Een deelstring ophalen

Doel: haal de landcode uit een telefoonnummer zonder +

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 |
| ...               |    |
Introductie tot Oracle SQL

Geneste functies

Doel: genereer gebruikersnamen uit de eerste 5 letters van de achternaam plus het 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  |
| ...      |            |          |
Introductie tot Oracle SQL

Andere handige tekstfuncties

LENGTH(val): geeft de lengte van een string

SELECT LENGTH('cat')
3

REPLACE(val, m, n): vervangt m door n in val

SELECT REPLACE('kayak', 'k', 'y')
yayay
Introductie tot Oracle SQL

Afronden

ROUND(column, m): rond column af op m decimalen

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     |
| ...       |        |       |
Introductie tot Oracle SQL

Afkorten

TRUNC(column, m): kort column af tot m decimalen

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   |
Introductie tot Oracle SQL

Modulo

MOD(column1, column2): geeft de rest van de deling

SELECT MOD(14, 4)
2
Introductie tot Oracle SQL

Modulo

MOD(column1, column2): geeft de rest van de deling

SELECT MOD(14, 2)
0
SELECT MOD(15, 2)
1
Introductie tot Oracle SQL

Modulo

Hebben we een even aantal werknemers?

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

Ja.

Introductie tot Oracle SQL

Laten we oefenen!

Introductie tot Oracle SQL

Preparing Video For Download...