Introduction to Oracle SQL
Hadrien Lacroix
Content Developer
Data types define what type of data a column can contain.
Numeric data | Character data | Date data | |
---|---|---|---|
AVG |
x | ||
SUM |
x | ||
MIN |
x | x | x |
COUNT |
x | x | x |
Conversion functions convert a column from one data type to another
SQL automatically converts data types
SELECT 'Track length: ' || Milliseconds
FROM Track
| 'Track length: ' || Milliseconds |
|--------------------------------------|
| Track length: 343719 |
| Track length: 342562 |
| ... |
Convert a number string to a character format using the TO_CHAR
function:
SELECT UnitPrice, TO_CHAR(UnitPrice, '$999.99')
FROM InvoiceLine
| UnitPrice | TO_CHAR(UnitPrice, '$9.99') |
|-------------|-----------------------------|
| 0.99 | $0.99 |
| 1.99 | $1.99 |
| ... | ... |
$
: Floating dollar sign.
: Decimal position9
: Specifies numeric position. The number of 9's determine the display width0
: Specifies leading zeros,
: Comma position in the numberConvert a date string to a character format using the TO_CHAR
function:
SELECT TO_CHAR(BirthDate, 'DD-MON-YYYY')
FROM Employee
| TO_CHAR(BirthDate, 'DD-MON-YYYY') |
|-----------------------------------|
| 19-SEP-1947 |
| ... |
YYYY
: Four digit representation of yearYEAR
: Year spelled outMM
: Two digit value of monthMONTH
: Full name of monthMON
: 3-letter representation of monthDY
: 3-letter representation of day of weekDAY
: Full name of the dayDD
: Numeric day of the monthConvert a character string to a number format using the TO_NUMBER
function:
SELECT TO_NUMBER('$15,000.75', '$999,999.99')
FROM DUAL
| TO_NUMBER('$15,000.75', '$999,999.99') |
|----------------------------------------|
| 15000.75 |
$
: Floating dollar sign.
: Decimal position9
: Specifies numeric position. The number of 9's determine the display width0
: Specifies leading zeros,
: Comma position in the numberConvert a character string to a date format using the TO_DATE
function:
SELECT TO_DATE('2016-01-31','YYYY-MM-DD')
FROM DUAL
| TO_DATE('2016-01-31','YYYY-MM-DD') |
|----------------------------------------|
| 31-JAN-16 |
YYYY
: Four digit representation of yearYEAR
: Year spelled outMM
: Two digit value of monthMONTH
: Full name of monthMON
: 3-letter representation of monthDY
: 3-letter representation of day of weekDAY
: Full name of the dayDD
: Numeric day of the month
Introduction to Oracle SQL