oracle sql built-in functions

Post on 31-Dec-2015

50 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Oracle SQL Built-in Functions. Chapter 5 in Lab Reference. Text Functions Concatenation operator ||. To concatenate column names with other column names or with literal characters. Example: Select name || ‘ has an id of ‘ || ssn From employee;. NAME||’HAS AN ID OF'||SSN - PowerPoint PPT Presentation

TRANSCRIPT

Oracle SQL Oracle SQL

Built-in FunctionsBuilt-in Functions

Chapter 5 in Lab Reference

Text Functions Concatenation operator ||

To concatenate column names with other column names or with literal characters.

Example:Example:

Select name || ‘ has an id of ‘ || ssn

From employee;

Built-in Functions 2

NAME||’HAS AN ID OF'||SSN------------------------------------------------------------------------------

Jamil N.Samir has an id of 123456789 Amani F.Zaki has an id of 999887777 Jihan H.Walid has an id of 987654321 Ramy S.Nabil has an id of 666884444 Joyce A.Eman has an id of 453453453 Ahmad V.Jabbar has an id of 987987987 James B.Baher has an id of 888665555

7 rows selected.

Column Alias Names

Example:Example:

Select name AS Employee

From employee;

Built-in Functions 3

EMPLOYEE-------------------------------

Jamil N.Samir Amani F.Zaki Jihan H.Walid Ramy S.Nabil Joyce A.Eman Ahmad V.Jabbar James B.Baher

7 rows selected.

Column Alias Names

When you want to include spaces or special characters in alias names, then enclose the alias name in double quotation marks.

Example:Example:

Select name || ' has an id of ' || ssn "Important information"

From employee;

Built-in Functions 4

IMPORTANT INFORMATION---------------------------------------------------------------------------------

Jamil N.Samir has an id of 123456789Amani F.Zaki has an id of 999887777Jihan H.Walid has an id of 987654321Ahmad V.Jabbar has an id of 987987987James B.Baher has an id of 888665555

7 rows selected.

Table Alias Names

Example:Example:

Select T.item_id, T.item_desc

From item T;

Built-in Functions 5

ITEM_ID ITEM_DESC --------------------------------------------------------- LA-101 Box, Small

NY-102 Bottle, Large

Number FunctionsROUND

The ROUND function rounds the value you want to modify.

Example:Example:

Select product_name, product_price, ROUND(product_price,0) From product;

Built-in Functions 6

PRODUCT_NAME PRODUCT_PRICE ROUND(PRODUCT_PRICE,0)----------------------------------------------------------------------------------------------------------------------------------------------

Roco Pencil 3.95 4

FABER Pen 5 5

Roco Pad 2.2 2

Number FunctionsTRUNC

The TRUNC function truncates precision from a number.

Example:Example:

Select product_name, product_price, TRUNC(product_price,0) From product;

Built-in Functions 7

PRODUCT_NAME PRODUCT_PRICE TRUNC(PRODUCT_PRICE,0)--------------------------------------------------------------------------------------------------------------------------------------------

Roco Pencil 3.95 3

FABER Pen 5 5

Roco Pad 2.2 2

Number FunctionsPOWER

power(m,n)

number m raised to the power of n.

Example:Example:

Select power(salary,2)

From employee

where ssn=123456789;

Built-in Functions 8

POWER(SALARY,2)-------------------------------------------

900000000

Number FunctionsSQRT

sqrt(n) returns square root of n.

Example:Example:

Select sqrt(salary)

From employee

where ssn=123456789;

Built-in Functions 9

SQRT(SALARY)--------------------------------------

173.20508

Text FunctionsUPPER, LOWER & INITCAP

These three functions change the case of the text you give them.

Example:Example:

Select UPPER(product_name)

From product;

Example:Example:

Select LOWER(product_name)

From product;

Built-in Functions 10

UPPER(PRODUCT_NAME) --------------------------------------------------------

ROCO PENCIL

FABER PEN ROCO PAD

LOWER(PRODUCT_NAME) --------------------------------------------------------

roco pencil faber pen roco pad

Example:Example:

Select INITCAP(product_name)

From product;

Built-in Functions 11

Text FunctionsUPPER, LOWER & INITCAP

INITCAP(PRODUCT_NAME)----------------------------------------------------------

Roco Pencil

Faber Pen Roco Pad

To determine the lengths of the data stored in a database column.

Example:Example:

Select product_name, LENGTH(product_name) AS “Name_Length”

From Product

where LENGTH(product_name)>8;

Built-in Functions 12

Text FunctionsLENGTH

PRODUCT_NAME NAME_LENGTH ---------------------------------------------------------------------------------

FABER Pen 9

Roco Pencil 11

Text FunctionsSUBSTR

To separate multiple bits of data into discrete segments.

Example:Example:

Select SUBSTR(item_id,1,2) Location, SUBSTR(item_id,4,3) Number, Item_desc

From item;

Built-in Functions 13

LOCATION NUMBER ITEM_DESC---------------------------------------------------------------------------------------------

LA 101 Box, Small NY 102 Bottle, Large

Text FunctionsREPLACE

Replace(char, str1, str2)

Every occurrence of str1 in char is replaced by str2.

Example:Example:

Select Replace(name,'Jamil','Sara')

From employee;

Built-in Functions 14

REPLACE(NAME,'JAMIL','SARA')-------------------------------------------------------------------

Sara N.Samir Amani F.Zaki Jihan H.Walid Ramy S.Nabil Joyce A.Eman Ahmad V.Jabbar James B.Baher

7 rows selected.

Date Functions

FunctionDescriptionSyntax

SysdateGet current system date and time.

INSERT INTO employee VALUES(…………, trunc(sysdate),……….);

Built-in Functions 15

Data Conversion Functions

Built-in Functions 16

FunctionDescription

To_char(input_value, format_code)Converts any data type to character data type.

To_number(input_value, format_code) Converts a valid set of numeric character data to number data type.

To_date(input_value, format_code)Converts character data of the proper format to date data type.

top related