This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Using Single-Row Functions to Using Single-Row Functions to Using Single-Row Functions to Using Single-Row Functions to Customize Output Customize Output Customize Output Customize Output
After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do the following:the following:the following:the following:• Describe various types of functions that are Describe various types of functions that are Describe various types of functions that are Describe various types of functions that are
available in SQLavailable in SQLavailable in SQLavailable in SQL• Use character, number, and date functions in Use character, number, and date functions in Use character, number, and date functions in Use character, number, and date functions in
SELECTSELECTSELECTSELECT statements statements statements statements• Describe the use of conversion functionsDescribe the use of conversion functionsDescribe the use of conversion functionsDescribe the use of conversion functions
Single-row functions:Single-row functions:Single-row functions:Single-row functions:• Manipulate data itemsManipulate data itemsManipulate data itemsManipulate data items• Accept arguments and Accept arguments and Accept arguments and Accept arguments and return one valuereturn one valuereturn one valuereturn one value• Act on each row that is returnedAct on each row that is returnedAct on each row that is returnedAct on each row that is returned• Return one result per rowReturn one result per rowReturn one result per rowReturn one result per row• May modify the data typeMay modify the data typeMay modify the data typeMay modify the data type((((隐式转换数据类型))))• Can be nestedCan be nestedCan be nestedCan be nested((((可以嵌套,多层嵌套))))• Accept arguments that can be a column or an Accept arguments that can be a column or an Accept arguments that can be a column or an Accept arguments that can be a column or an
These functions convert case for character strings:These functions convert case for character strings:These functions convert case for character strings:These functions convert case for character strings:
Using Case-Manipulation FunctionsUsing Case-Manipulation FunctionsUsing Case-Manipulation FunctionsUsing Case-Manipulation Functions
Display the employee number, name, and department Display the employee number, name, and department Display the employee number, name, and department Display the employee number, name, and department number for employee Higgins:number for employee Higgins:number for employee Higgins:number for employee Higgins:
These functions manipulate character strings:These functions manipulate character strings:These functions manipulate character strings:These functions manipulate character strings:
BLACK and BLUE BLACK and BLUE BLACK and BLUE BLACK and BLUE REPLACEREPLACEREPLACEREPLACE('JACK and JUE','J','BL') ('JACK and JUE','J','BL') ('JACK and JUE','J','BL') ('JACK and JUE','J','BL')
Using the Character-Manipulation Using the Character-Manipulation Using the Character-Manipulation Using the Character-Manipulation FunctionsFunctionsFunctionsFunctions
Number FunctionsNumber FunctionsNumber FunctionsNumber Functions
• ROUNDROUNDROUNDROUND: Rounds value to specified decimal: Rounds value to specified decimal: Rounds value to specified decimal: Rounds value to specified decimal• TRUNCTRUNCTRUNCTRUNC: Truncates value to specified decimal: Truncates value to specified decimal: Truncates value to specified decimal: Truncates value to specified decimal• MODMODMODMOD: Returns remainder of division: Returns remainder of division: Returns remainder of division: Returns remainder of division
Using the Using the Using the Using the ROUNDROUNDROUNDROUND Function Function Function Function
DUALDUALDUALDUAL is a dummy table that you can use to view results is a dummy table that you can use to view results is a dummy table that you can use to view results is a dummy table that you can use to view results from functions and calculations.from functions and calculations.from functions and calculations.from functions and calculations.
Using the Using the Using the Using the MODMODMODMOD Function Function Function Function
For all employees with job title of Sales Representative, For all employees with job title of Sales Representative, For all employees with job title of Sales Representative, For all employees with job title of Sales Representative, calculate the remainder of the salary after it is divided calculate the remainder of the salary after it is divided calculate the remainder of the salary after it is divided calculate the remainder of the salary after it is divided by 5,000.by 5,000.by 5,000.by 5,000.
SELECT last_name, hire_dateSELECT last_name, hire_dateSELECT last_name, hire_dateSELECT last_name, hire_dateFROM employeesFROM employeesFROM employeesFROM employeesWHERE hire_date < WHERE hire_date < WHERE hire_date < WHERE hire_date < ''''''''01-FEB-8801-FEB-8801-FEB-8801-FEB-88';';';';';';';';
Working with DatesWorking with DatesWorking with DatesWorking with Dates
• The Oracle database stores dates in an internal numeric The Oracle database stores dates in an internal numeric The Oracle database stores dates in an internal numeric The Oracle database stores dates in an internal numeric format: century, year, month, day, hours, minutes, and format: century, year, month, day, hours, minutes, and format: century, year, month, day, hours, minutes, and format: century, year, month, day, hours, minutes, and seconds.seconds.seconds.seconds.
• The default date display format is DD-MON-RR.The default date display format is DD-MON-RR.The default date display format is DD-MON-RR.The default date display format is DD-MON-RR.– Enables you to store 21st-century dates in theEnables you to store 21st-century dates in theEnables you to store 21st-century dates in theEnables you to store 21st-century dates in the
20th century by specifying only the last two digits 20th century by specifying only the last two digits 20th century by specifying only the last two digits 20th century by specifying only the last two digits of the year of the year of the year of the year
– Enables you to store 20th-century dates in theEnables you to store 20th-century dates in theEnables you to store 20th-century dates in theEnables you to store 20th-century dates in the21st century in the same way 21st century in the same way 21st century in the same way 21st century in the same way
Working with DatesWorking with DatesWorking with DatesWorking with Dates
SYSDATESYSDATESYSDATESYSDATE is a function that returns: is a function that returns: is a function that returns: is a function that returns:• Date Date Date Date • TimeTimeTimeTime
Arithmetic with DatesArithmetic with DatesArithmetic with DatesArithmetic with Dates
• Add or subtract a number to or from a date for a Add or subtract a number to or from a date for a Add or subtract a number to or from a date for a Add or subtract a number to or from a date for a resultant date value.resultant date value.resultant date value.resultant date value.
• Subtract two dates to find the number of days Subtract two dates to find the number of days Subtract two dates to find the number of days Subtract two dates to find the number of days between those dates.between those dates.between those dates.between those dates.
• Add hours to a date by dividing the number of Add hours to a date by dividing the number of Add hours to a date by dividing the number of Add hours to a date by dividing the number of hours by 24.hours by 24.hours by 24.hours by 24.
Date FunctionsDate FunctionsDate FunctionsDate Functions
Next day of the date specifiedNext day of the date specifiedNext day of the date specifiedNext day of the date specifiedNEXT_DAYNEXT_DAYNEXT_DAYNEXT_DAY
Last day of the monthLast day of the monthLast day of the monthLast day of the monthLAST_DAYLAST_DAYLAST_DAYLAST_DAY
Number of months between two datesNumber of months between two datesNumber of months between two datesNumber of months between two datesMONTHS_BETWEENMONTHS_BETWEENMONTHS_BETWEENMONTHS_BETWEEN
Add calendar months to dateAdd calendar months to dateAdd calendar months to dateAdd calendar months to dateADD_MONTHSADD_MONTHSADD_MONTHSADD_MONTHS
Practice 3: Overview of Part 1Practice 3: Overview of Part 1Practice 3: Overview of Part 1Practice 3: Overview of Part 1
This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:• Writing a query that displays the current date Writing a query that displays the current date Writing a query that displays the current date Writing a query that displays the current date • Creating queries that require the use of numeric, Creating queries that require the use of numeric, Creating queries that require the use of numeric, Creating queries that require the use of numeric,
character, and date functionscharacter, and date functionscharacter, and date functionscharacter, and date functions• Performing calculations of years and months of Performing calculations of years and months of Performing calculations of years and months of Performing calculations of years and months of
service for an employeeservice for an employeeservice for an employeeservice for an employee
Implicit Data Type ConversionImplicit Data Type ConversionImplicit Data Type ConversionImplicit Data Type Conversion
For assignments, the Oracle server can automatically For assignments, the Oracle server can automatically For assignments, the Oracle server can automatically For assignments, the Oracle server can automatically convert the following:convert the following:convert the following:convert the following:
Implicit Data Type ConversionImplicit Data Type ConversionImplicit Data Type ConversionImplicit Data Type Conversion
For expression evaluation, the Oracle Server can For expression evaluation, the Oracle Server can For expression evaluation, the Oracle Server can For expression evaluation, the Oracle Server can automatically convert the following:automatically convert the following:automatically convert the following:automatically convert the following:
NUMBERNUMBERNUMBERNUMBERVARCHAR2 or CHARVARCHAR2 or CHARVARCHAR2 or CHARVARCHAR2 or CHAR
DATEDATEDATEDATEVARCHAR2 or CHARVARCHAR2 or CHARVARCHAR2 or CHARVARCHAR2 or CHAR
ToToToToFromFromFromFrom
SELECT last_name, SELECT last_name, SELECT last_name, SELECT last_name, salarysalarysalarysalaryFROM employeesFROM employeesFROM employeesFROM employeesWHERE WHERE WHERE WHERE salary>'10000'salary>'10000'salary>'10000'salary>'10000'
Using the Using the Using the Using the TO_CHARTO_CHARTO_CHARTO_CHAR Function with Dates Function with Dates Function with Dates Function with Dates
The format model:The format model:The format model:The format model:• Must be enclosed by single quotation marksMust be enclosed by single quotation marksMust be enclosed by single quotation marksMust be enclosed by single quotation marks• Is case-sensitiveIs case-sensitiveIs case-sensitiveIs case-sensitive• Can include any valid date format elementCan include any valid date format elementCan include any valid date format elementCan include any valid date format element• Has an Has an Has an Has an fmfmfmfm element to remove padded blanks or element to remove padded blanks or element to remove padded blanks or element to remove padded blanks or
suppress leading zerossuppress leading zerossuppress leading zerossuppress leading zeros• Is separated from the date value by a commaIs separated from the date value by a commaIs separated from the date value by a commaIs separated from the date value by a comma
Elements of the Date Format ModelElements of the Date Format ModelElements of the Date Format ModelElements of the Date Format Model
Three-letter abbreviation of the day of the Three-letter abbreviation of the day of the Three-letter abbreviation of the day of the Three-letter abbreviation of the day of the weekweekweekweek
DYDYDYDY
Full name of the day of the weekFull name of the day of the weekFull name of the day of the weekFull name of the day of the weekDAYDAYDAYDAY
Two-digit value for monthTwo-digit value for monthTwo-digit value for monthTwo-digit value for monthMMMMMMMM
Full name of the monthFull name of the monthFull name of the monthFull name of the monthMONTHMONTHMONTHMONTH
Three-letter abbreviation of the monthThree-letter abbreviation of the monthThree-letter abbreviation of the monthThree-letter abbreviation of the monthMONMONMONMON
Numeric day of the monthNumeric day of the monthNumeric day of the monthNumeric day of the monthDDDDDDDD
Full year in numbersFull year in numbersFull year in numbersFull year in numbersYYYYYYYYYYYYYYYY
Year spelled out (in English)Year spelled out (in English)Year spelled out (in English)Year spelled out (in English)YEARYEARYEARYEAR
Elements of the Date Format ModelElements of the Date Format ModelElements of the Date Format ModelElements of the Date Format Model
• Time elements format the time portion of the date:Time elements format the time portion of the date:Time elements format the time portion of the date:Time elements format the time portion of the date:
• Add character strings by enclosing them in double Add character strings by enclosing them in double Add character strings by enclosing them in double Add character strings by enclosing them in double quotation marks:quotation marks:quotation marks:quotation marks:
• Number suffixes spell out numbers:Number suffixes spell out numbers:Number suffixes spell out numbers:Number suffixes spell out numbers:
DD "of" MONTHDD "of" MONTHDD "of" MONTHDD "of" MONTH 12 of OCTOBER12 of OCTOBER12 of OCTOBER12 of OCTOBER
Using the Using the Using the Using the TO_CHARTO_CHARTO_CHARTO_CHAR Function with Numbers Function with Numbers Function with Numbers Function with Numbers
These are some of the format elements that you can These are some of the format elements that you can These are some of the format elements that you can These are some of the format elements that you can use with the use with the use with the use with the TO_CHARTO_CHARTO_CHARTO_CHAR function to display a number function to display a number function to display a number function to display a number value as a character:value as a character:value as a character:value as a character:
Prints a decimal pointPrints a decimal pointPrints a decimal pointPrints a decimal point....
Prints a comma as thousands indicatorPrints a comma as thousands indicatorPrints a comma as thousands indicatorPrints a comma as thousands indicator,,,,
Places a floating dollar signPlaces a floating dollar signPlaces a floating dollar signPlaces a floating dollar sign$$$$
Uses the floating local currency symbolUses the floating local currency symbolUses the floating local currency symbolUses the floating local currency symbolLLLL
Represents a numberRepresents a numberRepresents a numberRepresents a number9999
Forces a zero to be displayedForces a zero to be displayedForces a zero to be displayedForces a zero to be displayed0000
Using the Using the Using the Using the TO_CHARTO_CHARTO_CHARTO_CHAR Function with Numbers Function with Numbers Function with Numbers Function with Numbers
Using the Using the Using the Using the TO_NUMBERTO_NUMBERTO_NUMBERTO_NUMBER and and and and TO_DATETO_DATETO_DATETO_DATE Functions Functions Functions Functions
• Convert a character string to a number format Convert a character string to a number format Convert a character string to a number format Convert a character string to a number format using the using the using the using the TO_NUMBERTO_NUMBERTO_NUMBERTO_NUMBER function: function: function: function:
• Convert a character string to a date format using Convert a character string to a date format using Convert a character string to a date format using Convert a character string to a date format using the the the the TO_DATETO_DATETO_DATETO_DATE function: function: function: function:
• These functions have an These functions have an These functions have an These functions have an fxfxfxfx modifier. This modifier. This modifier. This modifier. This modifier specifies the exact matching for the modifier specifies the exact matching for the modifier specifies the exact matching for the modifier specifies the exact matching for the character argument and date format model of a character argument and date format model of a character argument and date format model of a character argument and date format model of a TO_DATETO_DATETO_DATETO_DATE function. function. function. function.
Using the Using the Using the Using the TO_NUMBERTO_NUMBERTO_NUMBERTO_NUMBER and and and and TO_DATETO_DATETO_DATETO_DATE FunctionsFunctionsFunctionsFunctions
• Convert a character string to a number format Convert a character string to a number format Convert a character string to a number format Convert a character string to a number format using the using the using the using the TO_NUMBERTO_NUMBERTO_NUMBERTO_NUMBER function: function: function: function:
• Convert a character string to a date format using Convert a character string to a date format using Convert a character string to a date format using Convert a character string to a date format using the the the the TO_DATETO_DATETO_DATETO_DATE function: function: function: function:
• These functions have an These functions have an These functions have an These functions have an fxfxfxfx modifier. This modifier. This modifier. This modifier. This modifier specifies the exact matching for the modifier specifies the exact matching for the modifier specifies the exact matching for the modifier specifies the exact matching for the character argument and date format model of a character argument and date format model of a character argument and date format model of a character argument and date format model of a TO_DATETO_DATETO_DATETO_DATE function function function function
If two digits If two digits If two digits If two digits of the of the of the of the current current current current year are:year are:year are:year are:
0000––––49494949
0000––––49494949 50505050––––99999999
50505050––––99999999
The return date is in The return date is in The return date is in The return date is in the current centurythe current centurythe current centurythe current century
The return date is in The return date is in The return date is in The return date is in the century after the century after the century after the century after the current onethe current onethe current onethe current one
The return date is in The return date is in The return date is in The return date is in the century before the century before the century before the century before the current onethe current onethe current onethe current oneThe return date is in The return date is in The return date is in The return date is in the current centurythe current centurythe current centurythe current century
If the specified two-digit year is:If the specified two-digit year is:If the specified two-digit year is:If the specified two-digit year is:
Example of Example of Example of Example of RRRRRRRR Date Format Date Format Date Format Date Format
To find employees hired prior to 1990, use the To find employees hired prior to 1990, use the To find employees hired prior to 1990, use the To find employees hired prior to 1990, use the RRRRRRRR date date date date format, which produces the same results whether the format, which produces the same results whether the format, which produces the same results whether the format, which produces the same results whether the command is run in 1999 or now:command is run in 1999 or now:command is run in 1999 or now:command is run in 1999 or now:
• Single-row functions can be nested to any level.Single-row functions can be nested to any level.Single-row functions can be nested to any level.Single-row functions can be nested to any level.• Nested functions are evaluated from deepest level Nested functions are evaluated from deepest level Nested functions are evaluated from deepest level Nested functions are evaluated from deepest level
to the least deep level.to the least deep level.to the least deep level.to the least deep level.
General FunctionsGeneral FunctionsGeneral FunctionsGeneral Functions
The following functions work with any data type and The following functions work with any data type and The following functions work with any data type and The following functions work with any data type and pertain to using nulls:pertain to using nulls:pertain to using nulls:pertain to using nulls:• NVL (expr1, expr2)NVL (expr1, expr2)NVL (expr1, expr2)NVL (expr1, expr2)• NVL2 (expr1, expr2, expr3)NVL2 (expr1, expr2, expr3)NVL2 (expr1, expr2, expr3)NVL2 (expr1, expr2, expr3)• NULLIF (expr1, expr2)NULLIF (expr1, expr2)NULLIF (expr1, expr2)NULLIF (expr1, expr2)• COALESCE (expr1, expr2, ..., exprCOALESCE (expr1, expr2, ..., exprCOALESCE (expr1, expr2, ..., exprCOALESCE (expr1, expr2, ..., exprnnnn))))
Converts a null value to an actual value:Converts a null value to an actual value:Converts a null value to an actual value:Converts a null value to an actual value:• Data types that can be used are date, character, Data types that can be used are date, character, Data types that can be used are date, character, Data types that can be used are date, character,
and number.and number.and number.and number.• Data types must matchData types must matchData types must matchData types must match::::
SELECT last_name, salary, commission_pct,SELECT last_name, salary, commission_pct,SELECT last_name, salary, commission_pct,SELECT last_name, salary, commission_pct, NVL2(commission_pct, NVL2(commission_pct, NVL2(commission_pct, NVL2(commission_pct, 'SAL+COMM', 'SAL') income 'SAL+COMM', 'SAL') income 'SAL+COMM', 'SAL') income 'SAL+COMM', 'SAL') incomeFROM employees WHERE department_id IN (50, 80);FROM employees WHERE department_id IN (50, 80);FROM employees WHERE department_id IN (50, 80);FROM employees WHERE department_id IN (50, 80);
Using the Using the Using the Using the NVL2NVL2NVL2NVL2 Function Function Function Function
Using the Using the Using the Using the COALESCECOALESCECOALESCECOALESCE Function Function Function Function
• The advantage of the The advantage of the The advantage of the The advantage of the COALESCECOALESCECOALESCECOALESCE function over the function over the function over the function over the NVLNVLNVLNVL function is that the function is that the function is that the function is that the COALESCECOALESCECOALESCECOALESCE function can function can function can function can take multiple alternate values.take multiple alternate values.take multiple alternate values.take multiple alternate values.
• If the first expression is not null, the If the first expression is not null, the If the first expression is not null, the If the first expression is not null, the COALESCECOALESCECOALESCECOALESCE function returns that expression; otherwise, it function returns that expression; otherwise, it function returns that expression; otherwise, it function returns that expression; otherwise, it does a does a does a does a COALESCECOALESCECOALESCECOALESCE of the remaining expressions. of the remaining expressions. of the remaining expressions. of the remaining expressions.
SELECT last_name, SELECT last_name, SELECT last_name, SELECT last_name, COALESCE(manager_id,commission_pct, -1) comm COALESCE(manager_id,commission_pct, -1) comm COALESCE(manager_id,commission_pct, -1) comm COALESCE(manager_id,commission_pct, -1) comm FROM employees FROM employees FROM employees FROM employees ORDER BY commission_pct; ORDER BY commission_pct; ORDER BY commission_pct; ORDER BY commission_pct;
Using the Using the Using the Using the COALESCECOALESCECOALESCECOALESCE Function Function Function Function
• Provide the use of IF-THEN-ELSE logic within a Provide the use of IF-THEN-ELSE logic within a Provide the use of IF-THEN-ELSE logic within a Provide the use of IF-THEN-ELSE logic within a SQL statementSQL statementSQL statementSQL statement
• Use two methods:Use two methods:Use two methods:Use two methods:– CASECASECASECASE expression expression expression expression– DECODEDECODEDECODEDECODE function function function function
Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:
CASE CASE CASE CASE exprexprexprexpr WHEN WHEN WHEN WHEN comparison_expr1comparison_expr1comparison_expr1comparison_expr1 THEN THEN THEN THEN return_expr1return_expr1return_expr1return_expr1 [WHEN[WHEN[WHEN[WHEN comparison_expr2 comparison_expr2 comparison_expr2 comparison_expr2 THENTHENTHENTHEN return_expr2 return_expr2 return_expr2 return_expr2 WHEN WHEN WHEN WHEN comparison_exprn comparison_exprn comparison_exprn comparison_exprn THENTHENTHENTHEN return_exprn return_exprn return_exprn return_exprn ELSE ELSE ELSE ELSE else_exprelse_exprelse_exprelse_expr]]]]ENDENDENDEND
SELECT last_name, job_id, salary,SELECT last_name, job_id, salary,SELECT last_name, job_id, salary,SELECT last_name, job_id, salary, CASE job_id WHEN 'IT_PROG' THEN 1.10 CASE job_id WHEN 'IT_PROG' THEN 1.10 CASE job_id WHEN 'IT_PROG' THEN 1.10 CASE job_id WHEN 'IT_PROG' THEN 1.10****salarysalarysalarysalary WHEN 'ST_CLERK' THEN 1.15 WHEN 'ST_CLERK' THEN 1.15 WHEN 'ST_CLERK' THEN 1.15 WHEN 'ST_CLERK' THEN 1.15****salarysalarysalarysalary WHEN 'SA_REP' THEN 1.20 WHEN 'SA_REP' THEN 1.20 WHEN 'SA_REP' THEN 1.20 WHEN 'SA_REP' THEN 1.20****salarysalarysalarysalary ELSE salary END "REVISED_SALARY" ELSE salary END "REVISED_SALARY" ELSE salary END "REVISED_SALARY" ELSE salary END "REVISED_SALARY"FROM employees;FROM employees;FROM employees;FROM employees;
Using the Using the Using the Using the CASECASECASECASE Expression Expression Expression Expression
Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:
SELECT last_name, job_id, salary,SELECT last_name, job_id, salary,SELECT last_name, job_id, salary,SELECT last_name, job_id, salary,hire_date,hire_date,hire_date,hire_date, CASE CASE CASE CASE WHEN WHEN WHEN WHEN months_between(sysdate,hire_date)/12>=20months_between(sysdate,hire_date)/12>=20months_between(sysdate,hire_date)/12>=20months_between(sysdate,hire_date)/12>=20 THEN THEN THEN THEN 2.002.002.002.00****salarysalarysalarysalary WHEN WHEN WHEN WHEN job_id in (job_id in (job_id in (job_id in ('ST_CLERK''ST_CLERK''ST_CLERK''ST_CLERK',,,,'SA_REP'SA_REP'SA_REP'SA_REP')')')') THEN 1.15THEN 1.15THEN 1.15THEN 1.15****salarysalarysalarysalary WHEN WHEN WHEN WHEN salary<=5000salary<=5000salary<=5000salary<=5000 THEN 1.20 THEN 1.20 THEN 1.20 THEN 1.20****salarysalarysalarysalary ELSE salary END "REVISED_SALARY"ELSE salary END "REVISED_SALARY"ELSE salary END "REVISED_SALARY"ELSE salary END "REVISED_SALARY"FROM employees;FROM employees;FROM employees;FROM employees;
Using the Using the Using the Using the CASECASECASECASE Expression Expression Expression Expression
Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:an IF-THEN-ELSE statement:
DECODEDECODEDECODEDECODE Function Function Function Function
Facilitates conditional inquiries by doing the work of a Facilitates conditional inquiries by doing the work of a Facilitates conditional inquiries by doing the work of a Facilitates conditional inquiries by doing the work of a CASECASECASECASE expression or an IF-THEN-ELSE statement: expression or an IF-THEN-ELSE statement: expression or an IF-THEN-ELSE statement: expression or an IF-THEN-ELSE statement:
Using the Using the Using the Using the DECODEDECODEDECODEDECODE Function Function Function Function
Display the applicable tax rate for each employee in Display the applicable tax rate for each employee in Display the applicable tax rate for each employee in Display the applicable tax rate for each employee in department 80:department 80:department 80:department 80:
In this lesson, you should have learned how to: In this lesson, you should have learned how to: In this lesson, you should have learned how to: In this lesson, you should have learned how to: • Perform calculations on data using functionsPerform calculations on data using functionsPerform calculations on data using functionsPerform calculations on data using functions• Modify individual data items using functionsModify individual data items using functionsModify individual data items using functionsModify individual data items using functions• Manipulate output for groups of rows using Manipulate output for groups of rows using Manipulate output for groups of rows using Manipulate output for groups of rows using
functionsfunctionsfunctionsfunctions• Alter date formats for display using functionsAlter date formats for display using functionsAlter date formats for display using functionsAlter date formats for display using functions• Convert column data types using functionsConvert column data types using functionsConvert column data types using functionsConvert column data types using functions• Use Use Use Use NVLNVLNVLNVL functions functions functions functions• Use IF-THEN-ELSE logicUse IF-THEN-ELSE logicUse IF-THEN-ELSE logicUse IF-THEN-ELSE logic
Practice 3: Overview of Part 2Practice 3: Overview of Part 2Practice 3: Overview of Part 2Practice 3: Overview of Part 2
This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:• Creating queries that require the use of numeric, Creating queries that require the use of numeric, Creating queries that require the use of numeric, Creating queries that require the use of numeric,
character, and date functionscharacter, and date functionscharacter, and date functionscharacter, and date functions• Using concatenation with functionsUsing concatenation with functionsUsing concatenation with functionsUsing concatenation with functions• Writing case-insensitive queries to test the Writing case-insensitive queries to test the Writing case-insensitive queries to test the Writing case-insensitive queries to test the
usefulness of character functionsusefulness of character functionsusefulness of character functionsusefulness of character functions• Performing calculations of years and months of Performing calculations of years and months of Performing calculations of years and months of Performing calculations of years and months of
service for an employeeservice for an employeeservice for an employeeservice for an employee• Determining the review date for an employeeDetermining the review date for an employeeDetermining the review date for an employeeDetermining the review date for an employee
Q1:Q1:Q1:Q1: Which two are character manipulation functions? Which two are character manipulation functions? Which two are character manipulation functions? Which two are character manipulation functions? (Choose two.) (Choose two.) (Choose two.) (Choose two.) AAAA. TRIM . TRIM . TRIM . TRIM BBBB. REPLACE . REPLACE . REPLACE . REPLACE C. TRUNC C. TRUNC C. TRUNC C. TRUNC D. TO_DATE D. TO_DATE D. TO_DATE D. TO_DATE E. MOD E. MOD E. MOD E. MOD F. CASE F. CASE F. CASE F. CASE
Q2:Q2:Q2:Q2:A promotional sale is being advertised to the A promotional sale is being advertised to the A promotional sale is being advertised to the A promotional sale is being advertised to the customers in France. Which WHERE clause customers in France. Which WHERE clause customers in France. Which WHERE clause customers in France. Which WHERE clause identifies customers that are located in France? identifies customers that are located in France? identifies customers that are located in France? identifies customers that are located in France? A. WHERE lower(country_address) = "france" A. WHERE lower(country_address) = "france" A. WHERE lower(country_address) = "france" A. WHERE lower(country_address) = "france" BBBB. WHERE lower(country_address) = 'france' . WHERE lower(country_address) = 'france' . WHERE lower(country_address) = 'france' . WHERE lower(country_address) = 'france' C. WHERE lower(country_address) IS 'france' C. WHERE lower(country_address) IS 'france' C. WHERE lower(country_address) IS 'france' C. WHERE lower(country_address) IS 'france' D. WHERE lower(country_address) = '%france%' D. WHERE lower(country_address) = '%france%' D. WHERE lower(country_address) = '%france%' D. WHERE lower(country_address) = '%france%' E. WHERE lower(country_address) LIKE E. WHERE lower(country_address) LIKE E. WHERE lower(country_address) LIKE E. WHERE lower(country_address) LIKE %france% %france% %france% %france%
Q3: Q3: Q3: Q3: Which SELECT statement will get the result Which SELECT statement will get the result Which SELECT statement will get the result Which SELECT statement will get the result 'elloworld' from the string 'HelloWorld'? 'elloworld' from the string 'HelloWorld'? 'elloworld' from the string 'HelloWorld'? 'elloworld' from the string 'HelloWorld'?
A. SELECT SUBSTR( 'HelloWorld',1) FROM dual; A. SELECT SUBSTR( 'HelloWorld',1) FROM dual; A. SELECT SUBSTR( 'HelloWorld',1) FROM dual; A. SELECT SUBSTR( 'HelloWorld',1) FROM dual; B. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) B. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) B. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) B. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) FROM dual; FROM dual; FROM dual; FROM dual; C. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) C. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) C. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) C. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) FROM dual; FROM dual; FROM dual; FROM dual; D. SELECT LOWER(SUBSTR('HelloWorld', 2, 1) D. SELECT LOWER(SUBSTR('HelloWorld', 2, 1) D. SELECT LOWER(SUBSTR('HelloWorld', 2, 1) D. SELECT LOWER(SUBSTR('HelloWorld', 2, 1) FROM dual; FROM dual; FROM dual; FROM dual; EEEE. SELECT LOWER(TRIM ('H' FROM . SELECT LOWER(TRIM ('H' FROM . SELECT LOWER(TRIM ('H' FROM . SELECT LOWER(TRIM ('H' FROM 'HelloWorld')) FROM dual; 'HelloWorld')) FROM dual; 'HelloWorld')) FROM dual; 'HelloWorld')) FROM dual;
Q4:Q4:Q4:Q4:Evaluate the SQL statement:Evaluate the SQL statement:Evaluate the SQL statement:Evaluate the SQL statement:SELECT LPAD(salary,10,SELECT LPAD(salary,10,SELECT LPAD(salary,10,SELECT LPAD(salary,10,****) FROM EMP ) FROM EMP ) FROM EMP ) FROM EMP WHERE EMP_ID = 1001; WHERE EMP_ID = 1001; WHERE EMP_ID = 1001; WHERE EMP_ID = 1001;
If the employee with the EMP_ID 1001 has a salary If the employee with the EMP_ID 1001 has a salary If the employee with the EMP_ID 1001 has a salary If the employee with the EMP_ID 1001 has a salary of 17000, what is displayed? of 17000, what is displayed? of 17000, what is displayed? of 17000, what is displayed? A. 17000.00 A. 17000.00 A. 17000.00 A. 17000.00 B. 17000B. 17000B. 17000B. 17000******************** C. C. C. C. ****************170.00 170.00 170.00 170.00 D. D. D. D. ********17000.00 17000.00 17000.00 17000.00 EEEE. an error statement. an error statement. an error statement. an error statement
Q5:Q5:Q5:Q5:Evaluate the SQL statement:Evaluate the SQL statement:Evaluate the SQL statement:Evaluate the SQL statement:SELECT ROUND(TRUNC(MOD(1600,10),-1),2) SELECT ROUND(TRUNC(MOD(1600,10),-1),2) SELECT ROUND(TRUNC(MOD(1600,10),-1),2) SELECT ROUND(TRUNC(MOD(1600,10),-1),2) FROM dual; FROM dual; FROM dual; FROM dual;
WWWWhat hat hat hat will bewill bewill bewill be displayed? displayed? displayed? displayed? A. 0 A. 0 A. 0 A. 0 B. 1 B. 1 B. 1 B. 1 C. 0.00 C. 0.00 C. 0.00 C. 0.00 DDDD. . . . an error statement an error statement an error statement an error statement
Q6:Q6:Q6:Q6:Which SELECT statement should you use to Which SELECT statement should you use to Which SELECT statement should you use to Which SELECT statement should you use to extract the year from the system date and display it in extract the year from the system date and display it in extract the year from the system date and display it in extract the year from the system date and display it in the format "1998"? the format "1998"? the format "1998"? the format "1998"?
AAAA. SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual; . SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual; . SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual; . SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual; B. SELECT TO_DATE(SYSDATE,'yyyy') FROM dual; B. SELECT TO_DATE(SYSDATE,'yyyy') FROM dual; B. SELECT TO_DATE(SYSDATE,'yyyy') FROM dual; B. SELECT TO_DATE(SYSDATE,'yyyy') FROM dual; C. SELECT DECODE(SUBSTR(SYSDATE, 8), 'YYYY') C. SELECT DECODE(SUBSTR(SYSDATE, 8), 'YYYY') C. SELECT DECODE(SUBSTR(SYSDATE, 8), 'YYYY') C. SELECT DECODE(SUBSTR(SYSDATE, 8), 'YYYY') FROM dual; FROM dual; FROM dual; FROM dual; D. SELECT DECODE(SUBSTR(SYSDATE, 8), 'year') D. SELECT DECODE(SUBSTR(SYSDATE, 8), 'year') D. SELECT DECODE(SUBSTR(SYSDATE, 8), 'year') D. SELECT DECODE(SUBSTR(SYSDATE, 8), 'year') FROM dual; FROM dual; FROM dual; FROM dual; E. SELECT TO_CHAR(SUBSTR(SYSDATE, E. SELECT TO_CHAR(SUBSTR(SYSDATE, E. SELECT TO_CHAR(SUBSTR(SYSDATE, E. SELECT TO_CHAR(SUBSTR(SYSDATE, 8,2),'yyyy') 8,2),'yyyy') 8,2),'yyyy') 8,2),'yyyy') FROM dual; FROM dual; FROM dual; FROM dual;
Q7:Q7:Q7:Q7:Which three SELECT statements display 2000 in Which three SELECT statements display 2000 in Which three SELECT statements display 2000 in Which three SELECT statements display 2000 in the format "$2,000.00"?the format "$2,000.00"?the format "$2,000.00"?the format "$2,000.00"?
A. SELECT TO_CHAR(2000, '$#,###.##') FROM dual; A. SELECT TO_CHAR(2000, '$#,###.##') FROM dual; A. SELECT TO_CHAR(2000, '$#,###.##') FROM dual; A. SELECT TO_CHAR(2000, '$#,###.##') FROM dual; BBBB. SELECT TO_CHAR(2000, '$0,000.00') FROM dual; . SELECT TO_CHAR(2000, '$0,000.00') FROM dual; . SELECT TO_CHAR(2000, '$0,000.00') FROM dual; . SELECT TO_CHAR(2000, '$0,000.00') FROM dual; CCCC. SELECT TO_CHAR(2000, '$9,999.00') FROM dual; . SELECT TO_CHAR(2000, '$9,999.00') FROM dual; . SELECT TO_CHAR(2000, '$9,999.00') FROM dual; . SELECT TO_CHAR(2000, '$9,999.00') FROM dual; DDDD. SELECT TO_CHAR(2000, '$9,999.99') FROM dual; . SELECT TO_CHAR(2000, '$9,999.99') FROM dual; . SELECT TO_CHAR(2000, '$9,999.99') FROM dual; . SELECT TO_CHAR(2000, '$9,999.99') FROM dual; E. SELECT TO_CHAR(2000, '$2,000.00') FROM dual; E. SELECT TO_CHAR(2000, '$2,000.00') FROM dual; E. SELECT TO_CHAR(2000, '$2,000.00') FROM dual; E. SELECT TO_CHAR(2000, '$2,000.00') FROM dual; F. SELECT TO_CHAR(2000, '$N,NNN.NN') FROM F. SELECT TO_CHAR(2000, '$N,NNN.NN') FROM F. SELECT TO_CHAR(2000, '$N,NNN.NN') FROM F. SELECT TO_CHAR(2000, '$N,NNN.NN') FROM dual;dual;dual;dual;
Q8:Q8:Q8:Q8:Which SQL statement displays the date March 19, Which SQL statement displays the date March 19, Which SQL statement displays the date March 19, Which SQL statement displays the date March 19, 2001 in a format that appears as "Nineteenth of March 2001 in a format that appears as "Nineteenth of March 2001 in a format that appears as "Nineteenth of March 2001 in a format that appears as "Nineteenth of March 2001 12:00:00 AM"? 2001 12:00:00 AM"? 2001 12:00:00 AM"? 2001 12:00:00 AM"?