+ All Categories
Home > Documents > Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output...

Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output...

Date post: 23-Dec-2015
Category:
Upload: shauna-bell
View: 215 times
Download: 0 times
Share this document with a friend
30
Lecture 6 29/1/15
Transcript
Page 1: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Lecture 629/1/15

Page 2: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Number functions

• Number functions take numbers as input, change them, and output the results as numbers.

2

Page 3: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Round()

• This is used to round values up or down and to specify the number of decimal places. To see this, run

• Select round(123.4567,2), round(123.4567,3), round(1234.432,1)

from sys.dual;

• This will output

ROUND(123.4567,2) ROUND(123.4567,3) ROUND(1234.432,1)---------------- ----------------- -----------------123.46 123.457 1234.4

3

Page 4: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Trunc()

Truncating is similar to rounding. We specify the required number of decimal places but Oracle doesn’t round up or down. It simply “chops off” extra digits.• To see the difference, examine the following select round(123.456,2), trunc(123.456,2) from sys.dual;Will returnROUND(123.456,2) TRUNC(123.456,2)------------------------- ----------------------------123.46 123.45

4

Page 5: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Sign()

This is used to show if a value is zero, positive, or negative. 1 is returned if the number is positive� -1 is returned if the number is negative� 0 is returned if the number is zero�• i.e.

select sign(-11421.215) from sys.dualwill return –1.

5

Page 6: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

CEIL()

• Raises the value of the number to the next highest integer.• For example, Ceil(13213.4214)

Returns13214

6

Page 7: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Floor()

Lowers the value to the next lowest integer.For exampleFloor(123.89)Returns 123

7

Page 8: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Power() and others

• POWERRaises the number given to the power given.Power(12,2)Raises 12 to the power of 2.

select power(12,2)from sys.dual;Answer:

• OthersThere are other numerical functions which Oracle can use.They are straight forward and easy to use. Other functions includeSQRT (square root), ABS (absolute value), MOD (modulus), LOG (logarithmic), SIN (sine value), COS (cosine value), TAN (tangent value).There are several more.

8

Page 9: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Dual • DUAL is a table owned by the SYS user that contains a single

VARCHAR2 column called DUMMY and a single row with the value 'X' in it.

• This table is handy when you want to select a pseudocolumn such as SYSDATE or simply select an expression and only want to get a single row back.

SQL> DESC sys.dualName Null? Type------------------------------- -------- -----------------------DUMMY VARCHAR2(1) 9

Page 10: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

More character functions

Page 11: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Substr

• This stands for substring. It returns a part of a string. We specify which part of the string we want to return.• For example

select substr('Diploma',2,3) from sys.dual;

ipl

11

Page 12: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

INSTR()

INSTR() is used to find where characters occur in a string.• For example,• Instr(‘Diploma’,’o’)• Would return the number 5.

• select instr('Diploma','oma')• from sys.dual;Return?????

12

Page 13: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 14: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

INSTR() Continued..• We can change the syntax slightly. So far we have searched for

the 1st occurrence. We can also search for further occurrences.

For example

select instr('Seventy','e',3)from sys.dual;

14

Page 15: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 16: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Ltrim()

Ltrim() is used to remove leading occurrences of characters.• If we don’t specify a character, Oracle will remove leading spaces.• For exampleRunning ltrim(‘ Oracle’)Will remove the leading spaces.

Ltrim(‘spacious’,’s’)Will returnpacious (the leading s has been removed)

16

Page 17: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 18: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 19: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Ltrim() Continued..select Ltrim('spacious','p')from sys.dual;Will return ????

• The order specified for the leading characters is not important. For example,Ltrim(‘spacious’,’ps’) Is the same as Ltrim(‘spacious’,’sp’)

select Ltrim('spacious','sp')from sys.dual;

select Ltrim('spacious','ps')from sys.dual;

RTRIMIs the same as LTRIM, except it trims from the right.

select rtrim('spacious','su')from sys.dual;

Spacio

select rtrim('spacious','soui')from sys.dual;

spac19

Page 20: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 21: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Another Example

select ltrim(emp_name, 'H') from employee;

EARNEBYRNEWALSHARTEDOHERTYMARTIN;

21

Page 22: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Another example

SELECT LTRIM(emp_name, 'H' ) "Employee Name" FROM employee WHERE emp_name LIKE 'H%';

Employee NameEARNEARTE

22

Page 23: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

LPAD

23

• Lpad is used to “pad” columns/strings

to the left.• To see this let us take the following

string.• Let us say that we want the string to

appear as being10 characters in length. If we say that we want it to be padded to the left, it would appear like -

Page 24: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

LPAD Continued…If we padded with ‘*’it would look like this • The syntax for this would beLpad(‘diploma’,10,’*’)• Lpad the word ‘diploma’ so that it is 10 characters long, with extra spaces to the left being filled with *’s.

RPAD• Rpad, does the same, except that

it pads to the right.• What will the following command

do ?• Rpad (‘course’,12) 24

select Lpad('diploma',10,'*')from sys.dual;

***diploma

select rpad('diploma',10,'*')from sys.dual;

diploma***

Page 25: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

ExampleLPAD('tech', 7); would return ' tech'

LPAD('tech', 2); would return 'te'

LPAD('tech', 8, '0'); would return '0000tech'

LPAD('tech on the net', 15, 'z');

would return 'tech on the net'

LPAD('tech on the net', 16, 'z');

would return 'ztech on the net'

SELECT LPAD('Good',10,'.'), RPAD('Good',10,'.') FROM dual;

.......Good Good.......

25

Page 26: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Page 27: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Length()

• Length() returns the length of a string. For example

• select length('Oracle')• from sys.dual;

27

Page 28: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

TranslateTranslate is used to change characters. select translate('SMITH','I','O') from sys.dual;

Will change all letter I’s to letter O’s in the stringSMITH.

select translate('HEEEEEEEEEEELP','E','A') from sys.dual;

• We can also specify more than 1 character to translate.select translate('HEEEEEEEEEEELP','LP','AA') from sys.dual;

28

Page 29: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

REPLACE()

• Replace is similar to translate. With translate there must be a match between the number of characters to change and the number of characters to change with. I.e. we can’t replace X with TR. We can only replace 1 character with 1 character, 2 with 2, etc.

With replace we can do this.For exampleSELECT replace(job,'ANALYST','BUSANALYST') AS NEWTITLE from EMPLOYEE;

Will search the job column and replace all occurrences of ANALYST with BUSANALYST. 29

Page 30: Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Recommended