Single-Row Functions

Post on 31-Mar-2016

214 views 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 of Single-Row Functions

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

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

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

SUBSTR(‘HelloWorld’,1,5)

IT Portfolio, Class of 2013

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

LENGTH(‘HelloWorld’)

IT Portfolio, Class of 2013

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

INSTR(‘HelloWorld’, ‘W’)

IT Portfolio, Class of 2013

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

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

SELECT RPAD(salary,10,’$’)

IT Portfolio, Class of 2013

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

TRIM(‘H’ FROM ‘HelloWorld’)

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

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

IT Portfolio, Class of 2013

Add calendar months to date ADD_MONTHS(SYSDATE,6)

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

Last day of the month LAST_DAY(SYSDATE)

IT Portfolio, Class of 2013

SYSDATE returns the current date SELECT SYSDATE FROM dual

IT Portfolio, Class of 2013

Rounds date ROUND(sysdate, 'MONTH')

ROUND(sysdate, ‘YEAR’)

IT Portfolio, Class of 2013

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

IT Portfolio, Class of 2013

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

This converts a character string back to a number

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

IT Portfolio, Class of 2013

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

NVL NVL2 NULLIF COALESCE

IT Portfolio, Class of 2013

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

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

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

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

CASE DECODE

IT Portfolio, Class of 2013

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

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