single-row functions

35
IT Program Portfolio-Class of 2013-Battlefield High School IT Portfolio, Class of 2013

Upload: brian-potts

Post on 31-Mar-2016

214 views

Category:

Documents


0 download

DESCRIPTION

IT Program Portfolio-Class of 2013-Battlefield High School IT Portfolio, Class of 2013  A single row function is when a function is applied to only ONE row of data, and there is only ONE output. IT Portfolio, Class of 2013  UPPER makes the selected data in upper- case.  SELECT UPPER(last_name)  This could make Potts, POTTS IT Portfolio, Class of 2013  LOWER makes the selected data lower-case  SELECT LOWER(last_name)  ‘Potts’ to ‘potts’ IT Portfolio, Class of 2013

TRANSCRIPT

Page 1: Single-Row Functions

IT Program Portfolio-Class of 2013-Battlefield High School

IT Portfolio, Class of 2013

Page 2: Single-Row Functions

A single row function is when a function is applied to only ONE row of data, and there is only ONE output.

IT Portfolio, Class of 2013

Page 3: Single-Row Functions

UPPER makes the selected data in upper-case.

SELECT UPPER(last_name) This could make Potts, POTTS

IT Portfolio, Class of 2013

Page 4: Single-Row Functions

LOWER makes the selected data lower-case SELECT LOWER(last_name) ‘Potts’ to ‘potts’

IT Portfolio, Class of 2013

Page 5: Single-Row Functions

INITCAP makes selected data proper-case SELECT INITCAP(job_title) ‘manager’ to ‘Manager’.

IT Portfolio, Class of 2013

Page 6: Single-Row Functions

CONCAT brings together selected data SELECT CONCAT (‘Hello’,’World’) FROM dual

IT Portfolio, Class of 2013

Page 7: Single-Row Functions

SUBSTR is used to find a specific output based on a definite amount of values

SUBSTR(‘HelloWorld’,1,5)

IT Portfolio, Class of 2013

Page 8: Single-Row Functions

LENGTH will give you the amount of characters on a specified value

LENGTH(‘HelloWorld’)

IT Portfolio, Class of 2013

Page 9: Single-Row Functions

INSTR will give you a numerical amount from one point to another

INSTR(‘HelloWorld’, ‘W’)

IT Portfolio, Class of 2013

Page 10: Single-Row Functions

LPAD fills unused space with specified character, to the left of the selected value.

SELECT LPAD(salary,10,’$’) FROM employees

IT Portfolio, Class of 2013

Page 11: Single-Row Functions

RPAD fills unused space with specified character, to the right of the selected value.

SELECT RPAD(salary,10,’$’)

IT Portfolio, Class of 2013

Page 12: Single-Row Functions

TRIM removes characters at the beginning or end of a string.

TRIM(‘H’ FROM ‘HelloWorld’)

IT Portfolio, Class of 2013

Page 13: Single-Row Functions

Replaces a specified character with another SELECT REPLACE('Brian','Brian', 'Awesome') FROM dual

IT Portfolio, Class of 2013

Page 14: Single-Row Functions

Rounds value to specified amount. SELECT ROUND(5.657, 1)

IT Portfolio, Class of 2013

Page 15: Single-Row Functions

To truncate is to shorten or cut off a number. TRUNC(5.657, 1)

IT Portfolio, Class of 2013

Page 16: Single-Row Functions

MOD = remainder of one value/another value MOD(700,200)

IT Portfolio, Class of 2013

Page 17: Single-Row Functions

Functions pertaining to dates ◦ MONTHS_BETWEEN ◦ ADD_MONTHS ◦ NEXT_DAY ◦ LAST_DAY ◦ ROUND ◦ TRUNC

The SYSDATE used is 06-FEB-12 The date format is DD-MON-YY

IT Portfolio, Class of 2013

Page 18: Single-Row Functions

Number of months between two dates. MONTHS_BETWEEN(SYSDATE, ‘06-JAN-12’)

IT Portfolio, Class of 2013

Page 19: Single-Row Functions

Add calendar months to date ADD_MONTHS(SYSDATE,6)

IT Portfolio, Class of 2013

Page 20: Single-Row Functions

Next day of the date specified NEXT_DAY(SYSDATE,'FRIDAY')

IT Portfolio, Class of 2013

Page 21: Single-Row Functions

Last day of the month LAST_DAY(SYSDATE)

IT Portfolio, Class of 2013

Page 22: Single-Row Functions

SYSDATE returns the current date SELECT SYSDATE FROM dual

IT Portfolio, Class of 2013

Page 23: Single-Row Functions

Rounds date ROUND(sysdate, 'MONTH')

ROUND(sysdate, ‘YEAR’)

IT Portfolio, Class of 2013

Page 24: Single-Row Functions

Truncates date select trunc(to_date('22-AUG-12'), 'YEAR') from dual

IT Portfolio, Class of 2013

Page 25: Single-Row Functions

TO_CHAR is used to convert dates and numbers to an alternate specified format.

select to_char(sysdate, 'fmMonth dd, RRRR') from dual select to_char(1234, '00999') from dual

IT Portfolio, Class of 2013

Page 26: Single-Row Functions

This converts a character string back to a number

select to_number('1210.73', '9999.99') from dual

IT Portfolio, Class of 2013

Page 27: Single-Row Functions

Converts character string to date format TO_DATE(‘character string’, ‘format model’) select to_date('June 22, 1995', 'Month dd,

RRRR') from dual

IT Portfolio, Class of 2013

Page 28: Single-Row Functions

NVL NVL2 NULLIF COALESCE

IT Portfolio, Class of 2013

Page 29: Single-Row Functions

NVL detects if a value is null, and then replaces it if it is.

NVL(possibly null value, value to replace null) SELECT NVL(comments, 'no comment') from d_play_list_items

IT Portfolio, Class of 2013

Page 30: Single-Row Functions

This has 3 expressions. If the first value is not null, then the second is returned. If the first value is null, the third expression is returnded.

SELECT last_name, salary, NVL2(commission_pct, salary + (salary*commission_pct), salary) AS income

FROM employees

IT Portfolio, Class of 2013

Page 31: Single-Row Functions

This compares two expressions. If equal, the function returns null. If not equal, the function returns the first expression.

SELECT first_name, length(first_name) "Length FN", last_name, length(last_name) "LENGTH LN", NULLIF(length(first_name), length(last_name)) as "Compare Them"

FROM d_partners

IT Portfolio, Class of 2013

Page 32: Single-Row Functions

If the first expression is null, the function goes down the line until the first not null function is found.

SELECT last_name, COALESCE(commission_pct, salary, 10) FROM employees ORDER BY commission_pct

IT Portfolio, Class of 2013

Page 33: Single-Row Functions

CASE DECODE

IT Portfolio, Class of 2013

Page 34: Single-Row Functions

The CASE is the SQL version of an “if-then-else” statement SELECT id,loc_type, rental_fee,

CASE loc_type WHEN 'Private Home' THEN 'No Increase' WHEN 'Hotel' THEN 'Increase 5%' ELSE rental_fee END AS "Revised Fee" FROM d_venues

IT Portfolio, Class of 2013

Page 35: Single-Row Functions

Similar to the CASE function with the “if, then, else” intention SELECT department_id, last_name, salary,

NVL(DECODE(department_id, 10, 1.25*salary, 90, 1.5*salary, 130,1.75*salary), salary) AS "New Salary" FROM employees

IT Portfolio, Class of 2013