Download - Single Row Functions
![Page 1: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/1.jpg)
Single Row Functions
Week 2
![Page 2: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/2.jpg)
Objectives
– Describe types of single row functions in SQL
– Describe and use character, number, date, general and conversion SQL functions
![Page 3: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/3.jpg)
Functions
arg 1arg 1
arg 2arg 2
arg arg nn
FunctionFunction
ResultResult
• All functions input 0, 1 or more arguments and output a single result.
• 2 basic types in SQL: Single row and Group
![Page 4: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/4.jpg)
Single Row Functions
– Act on each individual row selected– Return one result per row retrieved from
table– Can be nested– Function can be used as part of expression
in: SELECT clause, WHERE clause, ORDER BY clause, …
– Argument for function may be any expression (literals, columns, arithmetic operators, …
![Page 5: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/5.jpg)
ConversionConversion
CharacterCharacter
NumberNumber
DateDate
GeneralGeneralSingle-row Single-row functionsfunctions
Types of Single Row Functions
![Page 6: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/6.jpg)
CharacterCharacterfunctionsfunctions
LOWERLOWER
UPPERUPPER
INITCAPINITCAP
CONCATCONCAT
SUBSTRSUBSTR
LENGTHLENGTH
INSTRINSTR
LPADLPAD
TRIMTRIM
Case conversion Case conversion functionsfunctions
Character manipulationCharacter manipulationfunctionsfunctions
Single Row Character Functions
![Page 7: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/7.jpg)
Case Conversion Functions
Function Result
LOWER('Intro to SQL')
UPPER('Intro to SQL')
INITCAP('Intro to SQL')
intro to sql
INTRO TO SQL
Intro to Sql
– Convert case of character string data – Useful for matching when unsure of case used for column’s data or when case varies by row by row– UPPER: converts all characters to upper case– LOWER: converts all characters to lower case– INITCAP: converts first character of each word to upper case and remaining to lower case
![Page 8: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/8.jpg)
SQL> SELECT employee_id, last_name, department_id FROM employeesWHERE last_name = ‘king';
no rows selected
SQL> SELECT employee_id, last_name, department_id FROM employeesWHERE last_name = ‘king';
no rows selected
EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90
SQL> SELECT employee_id, last_name, department_idFROM employeesWHERE LOWER(last_name) = ‘king';
SQL> SELECT employee_id, last_name, department_idFROM employeesWHERE last_name = UPPER(‘King‘);
no rows selected
SQL> SELECT employee_id, last_name, department_idFROM employeesWHERE last_name = UPPER(‘King‘);
no rows selected
Case Conversion ExamplesDisplay employee number, name and department number for employees named King
![Page 9: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/9.jpg)
EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90
SQL> SELECT employee_id, last_name, department_idFROM employeesWHERE INITCAP(last_name) = ‘King';
Case Conversion Examples (ctd)Display employee number, name and department number for employees named King
EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90
SQL> SELECT empno, last_name, department_idFROM employeesWHERE UPPER(last_name) = ‘KING';
![Page 10: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/10.jpg)
Case Conversion (ctd)Recommended: always use a case conversion function with column character data (except for unusual circumstances where case is significant in the data) and also always use a case conversion function with character substitution variables (except for unusual circumstances where case is significant in the data)
EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90
SQL> SELECT employee_id, last_name, department_id FROM employeesWHERE UPPER(last_name) = UPPER(‘&last_name’);
![Page 11: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/11.jpg)
Character Manipulation Functions• CONCAT(string1, string2)Joins 2 character strings together
(You are limited to using only two parameters with CONCAT)
Character Manipulation Functions
• SUBSTR(string, start_position, length)Extracts a string of determined length from a specified starting position
Character Manipulation Functions
• LENGTH(string)Shows the length of a string as a numeric value
Character Manipulation Functions
• INSTR(string, character)Finds numeric position of first occurrence of a specified character
Character Manipulation Functions
• LPAD(string, length, character)Places occurrences of a character (a blank is the default character) to the left of a string to end up with a specified length of character string
Character Manipulation Functions
• RPAD: pads a character value right-justified on a string
Character Manipulation Functions
• TRIM([{LEADING | TRAILING | BOTH}] character FROM string): removes occurrences of a leading and/or trailing (default is both leading and trailing) character (a blank is the default character) from a string
![Page 12: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/12.jpg)
CONCAT('Good', 'Day')
SUBSTR('Good',3,2)
LENGTH('Good')
INSTR('Good', 'o')
LPAD(last_name,20,'*')
TRIM(‘$‘ FROM ‘$2,345‘)
GoodDay
od
4
2
****************King
2,345
Function Result
Examples using Character Manipulation Functions
![Page 13: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/13.jpg)
Number Functions– Manipulate numeric values; frequently used functions Manipulate numeric values; frequently used functions include: ROUND, TRUNC, MODinclude: ROUND, TRUNC, MOD
ROUND(number, n): rounds number to n decimal placesROUND(number, n): rounds number to n decimal places
ROUND(43.826, 2)ROUND(43.826, 2) 43.8343.83
ROUND(43.826, 0)ROUND(43.826, 0) 4444
ROUND(43.826, -1)ROUND(43.826, -1) 4040
TRUNC(number, n):TRUNC(number, n): truncates value to n decimal placestruncates value to n decimal places
TRUNC(43.826, 2)TRUNC(43.826, 2) 43.8243.82
TRUNC(43.826, 0)TRUNC(43.826, 0) 4343
TRUNC(43.826, -1)TRUNC(43.826, -1) 4040
MOD(number1, number2): returns remainder of MOD(number1, number2): returns remainder of number1 divided by number2number1 divided by number2
MOD(17, 3)MOD(17, 3) 22
![Page 14: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/14.jpg)
Date Functions– manipulate date data: most perform calculations on datesmanipulate date data: most perform calculations on dates
• ADD_MONTHS(date, number): add or subtract a ADD_MONTHS(date, number): add or subtract a number of months from a datenumber of months from a date
ADD_MONTHS(’22-JAN-01’,6)ADD_MONTHS(’22-JAN-01’,6) 22-JUL-0122-JUL-01
• MONTHS_BETWEEN(date1, date2) : number of months MONTHS_BETWEEN(date1, date2) : number of months between datesbetween dates
MONTHS_BETWEEN(’22-JAN-01’, ’22-JUL-01)MONTHS_BETWEEN(’22-JAN-01’, ’22-JUL-01) -6 -6
• NEXT_DAY(date, day) returns the date for the next NEXT_DAY(date, day) returns the date for the next ‘day of the week’ from the date specified‘day of the week’ from the date specified
NEXT_DAY(’22-JAN-01’,’FRIDAY’)NEXT_DAY(’22-JAN-01’,’FRIDAY’) 26-JAN-01 26-JAN-01
• LAST_DAY(date) returns the last day of the month for the LAST_DAY(date) returns the last day of the month for the date givendate given
LAST_DAY(’22-JAN-01’)LAST_DAY(’22-JAN-01’) 31-JAN-01 31-JAN-01
![Page 15: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/15.jpg)
Date Functions (ctd)• ROUND function can also be used on dates, rounding ROUND function can also be used on dates, rounding a date to the nearest month or yeara date to the nearest month or year
ROUND(’22-JAN-01’, ‘MONTH’)ROUND(’22-JAN-01’, ‘MONTH’) 01-FEB-0101-FEB-01
ROUND(’22-JAN-01’,’YEAR’)ROUND(’22-JAN-01’,’YEAR’) 01-JAN-0101-JAN-01
• TRUNC function can also be used on dates, truncating a TRUNC function can also be used on dates, truncating a date to the nearest month or yeardate to the nearest month or year
TRUNC(’22-JAN-01’, ‘MONTH’)TRUNC(’22-JAN-01’, ‘MONTH’) 01-JAN-0101-JAN-01
TRUNC(’22-JAN-01’,’YEAR’)TRUNC(’22-JAN-01’,’YEAR’) 01-JAN-0101-JAN-01
![Page 16: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/16.jpg)
General Functions• NVL(column, value): used to provide an actual value in NVL(column, value): used to provide an actual value in place of a NULL value for a columnplace of a NULL value for a column
SELECT employee_id, sal, comm, (NVL(sal,1000) + SELECT employee_id, sal, comm, (NVL(sal,1000) + NVL(comm,0)) AS COMPENSATION NVL(comm,0)) AS COMPENSATION
FROM employeesFROM employees
• DECODE(expression, condition1, result1 [, condition2, DECODE(expression, condition1, result1 [, condition2, result2], … [, default]): operates similarly to an IF- THEN-result2], … [, default]): operates similarly to an IF- THEN-ELSE statement in evaluating values in a columnELSE statement in evaluating values in a column
SELECT employee_id, job, DECODE(job,’President’,1, ‘Manager’, SELECT employee_id, job, DECODE(job,’President’,1, ‘Manager’, 2, ‘Developer’, 3, ‘Analyst’, 3, ‘Clerk’, 4) AS “JOB RANKING”2, ‘Developer’, 3, ‘Analyst’, 3, ‘Clerk’, 4) AS “JOB RANKING”
FROM employeesFROM employees
• SYSDATE: returns current system dateSYSDATE: returns current system date
SELECT employee_id, hiredateSELECT employee_id, hiredate
FROM employeesFROM employees
WHERE hiredate < ADD_MONTHS(SYSDATE, – 6)WHERE hiredate < ADD_MONTHS(SYSDATE, – 6)
![Page 17: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/17.jpg)
Conversion Functions• many conversions in SQL occur implicitly, for example many conversions in SQL occur implicitly, for example string || numeric string || numeric string string• however in some cases a conversion function must be however in some cases a conversion function must be usedused
• TO_NUMBER(string, [format]): converts strings into TO_NUMBER(string, [format]): converts strings into numeric data - often used if numeric values have been numeric data - often used if numeric values have been stored using formatting of decimal points and numeric stored using formatting of decimal points and numeric separators; format specifies format of input dataseparators; format specifies format of input data
TO_NUMBER(‘1,234.56’, ‘9,999.99’)TO_NUMBER(‘1,234.56’, ‘9,999.99’)
• TO_CHAR(numeric data, [‘format’]): converts numeric TO_CHAR(numeric data, [‘format’]): converts numeric data to character data – normally used to format numeric data to character data – normally used to format numeric data (if format is not large enough to represent data then data (if format is not large enough to represent data then # symbols will be shown); format specifies output format# symbols will be shown); format specifies output format
TO_CHAR(sal, ‘$99,999.99’) TO_CHAR(sal, ‘$99,999.99’)
![Page 18: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/18.jpg)
Conversion Functions(ctd)• TO_CHAR(date data, [‘format’]): converts date data to TO_CHAR(date data, [‘format’]): converts date data to character data – normally used to customize format of character data – normally used to customize format of date datadate data
TO_CHAR(’22-JAN-01’, ‘DAY MONTH DD, YYYY’) TO_CHAR(’22-JAN-01’, ‘DAY MONTH DD, YYYY’) THURSDAY JANUARY 22, 2001THURSDAY JANUARY 22, 2001
Date Format Symbols used in Date ConversionDate Format Symbols used in Date ConversionDay:Day: Day of the weekDay of the weekDY:DY: Abbreviated day of the weekAbbreviated day of the weekDD: DD: 2 digit day value2 digit day valueMM: MM: 2 digit month value2 digit month valueMON:MON: Abbreviated month nameAbbreviated month nameMONTH:MONTH: Month nameMonth nameYYYY:YYYY: 4 digit year value4 digit year valueYEAR:YEAR:Year value in wordsYear value in wordsQ:Q: Quarter of the year (1Quarter of the year (1stst – 4 – 4thth))
![Page 19: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/19.jpg)
Conversion Functions (ctd)• TO_DATE(string, ‘format’): converts strings into data in TO_DATE(string, ‘format’): converts strings into data in Oracle’s internal date formatOracle’s internal date format
SELECT employee_id, hiredateSELECT employee_id, hiredate
FROM employeesFROM employees
WHERE hiredate = TO_DATE(‘September 28, WHERE hiredate = TO_DATE(‘September 28, 1981’, ‘Month DD, YYYY’)1981’, ‘Month DD, YYYY’)
![Page 20: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/20.jpg)
Nesting of Functions• Single row function calls can be nested within each Single row function calls can be nested within each otherother
• Innermost function is evaluated firstInnermost function is evaluated first
• Example – show the review dates for all employees Example – show the review dates for all employees where review date is first Monday occurring 3 months after where review date is first Monday occurring 3 months after starting work:starting work:
SELECT employee_id, NEXT_DAY(ADD_MONTHS(hiredate, 3) , SELECT employee_id, NEXT_DAY(ADD_MONTHS(hiredate, 3) , ‘MONDAY’) AS “FIRST REVIEW DATE”, (sal + NVL(comm,0)) AS ‘MONDAY’) AS “FIRST REVIEW DATE”, (sal + NVL(comm,0)) AS COMPENSATIONCOMPENSATION
FROM employeesFROM employees
![Page 21: Single Row Functions](https://reader035.vdocument.in/reader035/viewer/2022062517/56813854550346895d9ffc6b/html5/thumbnails/21.jpg)
Querying Data Not Found in a Table
• Sometimes you may want to display data not stored in a Sometimes you may want to display data not stored in a tabletable
• However format of SELECT statement requires a FROM However format of SELECT statement requires a FROM clauseclause
• In these situations refer to a small public table called In these situations refer to a small public table called DUAL which consists of one row of data for a single column DUAL which consists of one row of data for a single column and which therefore results in a single row of outputand which therefore results in a single row of output
• Example: display today’s date:Example: display today’s date:
SELECT SYSDATESELECT SYSDATE
FROM DUALFROM DUAL