Presentation is loading. Please wait.

Presentation is loading. Please wait.

4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.

Similar presentations


Presentation on theme: "4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running."— Presentation transcript:

1 4/2/16

2 Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running ltrim(‘ Oracle’) Will remove the leading spaces. Ltrim(‘spacious’,’s’) Will return pacious (the leading s has been removed) 2

3

4

5 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; RTRIM Is the same as LTRIM, except it trims from the right. select rtrim('spacious','su') from sys.dual; Output - Spacio select rtrim('spacious','soui') from sys.dual; Output - spac 5

6

7 select ltrim(emp_name, 'H') from employee; EARNE BYRNE WALSH ARTE DOHERTY MARTIN; 7 select ltrim(ename, 'M') from emp;

8 SELECT LTRIM(ename, 'M' ) "Employee Name" FROM emp WHERE ename LIKE 'M%'; Employee Name ILLER ARTIN 8

9 SELECT product_name, LTRIM(product_name, 'Monitor ') "Short Name" FROM products WHERE product_name LIKE 'Monitor%';  PRODUCT_NAME Short Name -------------------- --------------- Monitor 17/HR 17/HR Monitor 17/HR/F 17/HR/F Monitor 17/SD 17/SD Monitor 19/SD 19/SD Monitor 19/SD/M 19/SD/M Monitor 21/D 21/D Monitor 21/HR 21/HR Monitor 21/HR/M 21/HR/M Monitor 21/SD 21/SD Monitor Hinge - HD Hinge - HD Monitor Hinge - STD Hinge - STD 9

10 10 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 -

11 If we padded with ‘*’it would look like this The syntax for this would be Lpad(‘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) 11 select Lpad('diploma',10,'*') from sys.dual; ***diploma select rpad('diploma',10,'*') from sys.dual; diploma***

12 LPAD('tech', 7);' tech' LPAD('tech', 2);'te' LPAD('tech', 8, '0');'0000tech' LPAD('tech on the net', 15, 'z');'tech on the net' LPAD('tech on the net', 16, 'z');'ztech on the net' SELECT LPAD('Good',10,'.'), RPAD('Good',10,'.') FROM dual;.......GoodGood....... 12

13

14  Length() returns the length of a string. For example select length('Oracle') from sys.dual; 14

15  Answer 6

16 Translate 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 string SMITH. 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;  HEEEEEEEEEEEAA 16

17  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. For example SELECT replace(job,'ANALYST','BUSANALYST') AS NEWTITLE from EMPLOYEE; Will search the job column and replace all occurrences of ANALYST with BUSANALYST. NEWTITLE CLERK SALESMAN PRESIDENT BUSANALYST MANAGER SALESMAN MANAGER SALESMAN CLERK 17

18

19  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 pseudo column such as SYSDATE or simply select an expression and only want to get a single row back. SQL> DESC sys.dual Name Null? Type ------------------------------- -------- ------- ---------------- DUMMY VARCHAR2(1) 19

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

21  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.46123.4571234.4 21

22 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 return ROUND(123.456,2) TRUNC(123.456,2) ------------------------- ----------------- ----------- 123.46 123.45 22

23 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.dual will return –1. 23

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

25 Lowers the value to the next lowest integer. For example Floor(123.89) Returns 123 25

26 POWER Raises 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: Others There are other numerical functions which Oracle can use. They are straight forward and easy to use. Other functions include SQRT (square root), ABS (absolute value), MOD (modulus), LOG (logarithmic), SIN (sine value), COS (cosine value), TAN (tangent value). There are several more. 26

27 27 Language to create and modify data Data Definition Language Language to process and update data Data Manipulation Language An electronic document that provides detailed information about each and every piece of data in the database Data Dictionary Software that generates reports and makes the database user- friendly Reports and Utilities DBMS

28  Select * ◦ From Table Name ◦ Where ◦ Order by ◦ Wildcard ‘%’  Single Row Functions ◦ Character Functions – Lower(), Upper(), Instr(), Substr(), Initcap(), Concat()/||, lpad(), rpad(), ; ltrim(), rtrim() ◦ Number Functions – Round(), Trunc(), Ceil(), Floor(), Power() 28

29  Data Definition Language (DDL) ◦ Create CREATE TABLE EMP2 ( EMP_NO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2) ); ◦ Insert  INSERT INTO table (column1, column2,... ) VALUES (expression1, expression2,... ); ◦ Update  UPDATE table SET column1 = expression1, column2 = expression2,... WHERE conditions; ◦ Delete  DELETE FROM table WHERE conditions; ◦ Drop  DROP TABLE Table Name; 29

30  SYSDATE is a pseudo-column. It is used to retrieve the current date and time.  We normally select sysdate from a dummy table called sys.dual. ◦ Select sysdate from sys.dual; 20-JAN-14 30

31  We can add and subtract from dates. The syntax would be, as an example ◦ Select hiredate-7 from employee;  This would return the hiredate minus 1 week (7 days)  select hiredate from employee; 07-JAN-11 20-FEB-09 11-OCT-07 20-JAN-12 28-JUN-11 07-S Select hiredate-7 from employee; 31-DEC-10 13-FEB-09 04-OCT-07 13-JAN-12 21-JUN-11 31-AUG-13EP-13 31

32  MONTHS_BETWEEN  This tells us the number of months between 2 dates.  For example, to determine how many months an employee has worked is the company, we would run  ADD_MONTHS  We use this to add a number of months to a date.  What does the following do? select hiredate, add_months(hiredate,-3), add_months(hiredate,3) from employee;  Select months_between(sysdate,hiredate)"WIT H COMPANY", emp_name from employee; 36.434133064516129032258064516129 03225806HEARNE 59BYRNE 75.305100806451612903225806451612 90322581WALSH 24HARTE 30.756713709677419354838709677419 35483871DOHERTY 4.4341330645161290322580645161290 3225806MARTIN 32

33  Select trunc(months_between(sysdate,hireda te))"WITH COMPANY", emp_name from employee; 36HEARNE 59BYRNE 75WALSH 24HARTE 63CASEY 23MURRAY 30DOHERTY 4MARTIN 33

34 select hiredate, add_months(hiredate,3) from employee; 07-JAN-1107-APR-11 20-FEB-0920-MAY-09 11-OCT-0711-JAN-08 20-JAN-1220-APR-12 28-JUN-1128-SEP-11 07-SEP-1307-DEC-13 select hiredate, add_months(hiredate,-3), add_months(hiredate,3) from employee; 07-JAN-1107-OCT-10 07-APR-11 20-FEB-0920-NOV-08 20-MAY-09 11-OCT-0711-JUL-07 11-JAN-08 20-JAN-1220-OCT-11 20-APR-12 28-JUN-1128-MAR-11 28-SEP-11 07-SEP-1307-JUN-13 07-DEC-13 34

35  NEXT_DAY  To find the next occurrence of a particular day, we run next_day. For example, ◦ select sysdate, next_day(sysdate,'FRID AY') from sys.dual;  will tell us next Friday’s date. 20-JAN-1424-JAN-14  LAST_DAY  It is common in companies that pay day is the last day of the month. To determine this, we use last_day. ◦ select last_day(sysdate) from sys.dual;  will give us the date of the last day of this month. 31-JAN-14 35


Download ppt "4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running."

Similar presentations


Ads by Google