Single Row Functions Objectives • Explain the various types of functions available in SQL. • • Identify the basic concepts of using functions. • Explain the conversion functions and how they might be used. Include a variety of character, number, and data functions in SELECT statements. 3-2 Overview of Functions in SQL Use functions to • • • • • Perform calculations on data. Modify individual data items. Manipulate output for groups of rows. Alter date formats for display. Convert column datatypes. 3-3 Two Types of SQL Functions • Single row functions – Character – Number – Date – Conversion • SINGLE ROW Multiple row functions – Group 3-4 FUNCTION MULTI ROW Single Row Functions: Syntax Single row functions • • • • • • Manipulate data items. Accept arguments and return one value. Act on each row returned. Return one result per row. Modify the datatype. Can be nested. Syntax function_name (column|expression, [arg1, arg2,...]) 3-5 Character Functions • LOWER • UPPER • INITCAP • CONCAT • SUBSTR • LENGTH 3-6 Converts to lowercase Converts to uppercase Converts to initial capitalization Concatenates values Returns substring Returns number of characters Example Using Character Functions Display the fist name and last name of all employees with the last name of Patel. SELECT first_name, last_name FROM s_emp WHERE last_name = 'PATEL'; no rows returned SELECT FROM WHERE first_name, last_name s_emp UPPER(last_name) = 'PATEL'; FIRST_NAME -------------------Vikram Radha 3-7 LAST_NAME -------------------Patel Patel Number Functions • • • 3-8 ROUND Rounds value to specified decimal TRUNC Truncates value to specified decimal MOD Returns remainder of division Conversion Functions: Overview • TO_CHAR converts a number or date string to a character string. • TO_NUMBER converts a character string containing digits to a number. • TO_DATE converts a character string of a date to a date value. • Conversion functions can use a format model composed of many elements. 3-9 Nesting Single Row Functions • • Single row functions can be nested to any level. Nested functions are evaluated from deepest level to the least deep level. F3(F2(F1(col,arg1),arg2),arg3) Step 1 = Result 1 Step 2 = Result 2 Step 3 = Result 3 3-10