Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions."— Presentation transcript:

1 Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions

2 Topics for Today Functions String Functions (Pages 117 – 120)‏ Date and Time Functions (Pages 120 – 123)‏ Control Flow Functions (Page 125)‏ Comparison Functions (Pages 125 – 126)‏ Numerical Functions (Not in book)‏

3 Next Tuesday UNION [ALL | DISTINCT] Review topics for midterm Slightly modified movie archive database Talk about final exam database a little bit

4 What is a Function? Portion of prewritten code in MySQL used to perform a very specific task A function in SQL consists of: Zero or more input values A named function identifier One output value Functions in SQL are useful for: Formatting and extracting data Mathematical computing, converting data

5 Function Syntax Syntax: function_name(argument_list)‏ argument_list = [arg1, arg2, arg3,...] function_name is case insensitive Examples: SELECT LENGTH('STEVEN'); -- output is 6 SELECT SIN(3.14159); -- output is 2.6e-6 SELECT CURDATE(); -- output is 2008-10-13

6 Function Tips Don't Try to Memorize Everything Too many functions Every DBMS vendor uses different functions Look them up in the book, on the internet, etc. Get Parentheses Right Type in parentheses first, then fill in the arguments Nested Functions Type each function in separately in notepad When done, substitute nested functions back in

7 Function Usage You can use functions in most SQL clauses SELECT clause (yes, and very common) FROM clause (no) ON clause (yes) WHERE clause (yes, and very common) GROUP BY clause (yes) HAVING clause (yes) ORDER BY clause (yes)

8 Strings and String Functions Unlike strings in Java, C, C++, and C#, SQL strings use indexes that start from 1 's t e v e n' 1 2 3 4 5 6 (SQL indexes) 0 1 2 3 4 5 (C++ indexes) To extract 'even' from 'steven' we must start at position 3 and extract 4 characters SELECT SUBSTRING('steven', 3, 4)

9 String Functions CHAR_LENGTH(str), LENGTH(str)‏‏ CONCAT(str1, str2,...)‏ CONCAT_WS(separator, str1, str2,...)‏ FORMAT(number, decimal_places)‏ LEFT(str, len), RIGHT(str, len)‏ LOCATE(substr, str), LOCATE(substr, str, pos)‏ LOWER(str), UPPER(str)‏

10 More String Functions SUBSTRING(str, pos, len)‏ SUBSTRING_INDEX(str, delim, count)‏ TRIM(str), LTRIM(str), RTRIM(str)‏ More string functions can be found here.here.

11 About Dates You can do math on date values, using the INTERVAL keyword SELECT '1973-08-13' + INTERVAL 10 YEAR; SELECT '1973-08-13' - INTERVAL 10 YEAR; Valid intervals are: DAY MONTH YEAR

12 About Date Times Time information can be appended to a date in the following way (place a space between the date and the time): '1970-04-19 06:50:15' April 19, 1970 at 6:50:15 am You can do math on date times as well: SELECT '1973-08-13 06:50:15' + INTERVAL 10 MINUTE; Extra valid intervals for time include: HOUR, MINUTE and SECOND

13 More About Interval Math You can chain interval additions and subtractions SELECT '1973-08-13 06:50:15' + INTERVAL 10 MINUTE + INTERVAL 1 YEAR;

14 Date and Time Functions CURDATE()‏ DATEDIFF(date1, date2)‏ DATE_ADD(date, INTERVAL value type)‏ DATE_SUB(date, INTERVAL value type)‏ DATE_FORMAT(date, format_string) DAY(date)‏, WEEK(date) MONTH(date), YEAR(date)

15 More Date and Time Functions DAYOFMONTH(date) DAYOFWEEK(date)‏ DAYOFYEAR(date)‏ LAST_DAY(date)‏ NOW()‏ TO_DAYS(date)‏ More date and time functions can be found here.here

16 Control Flow Functions IF(condition, true_expr, false_expr)‏ IFNULL(expr1, expr2)‏ CASE WHEN expr1 IS NULL THEN expr2 ELSE expr1 END NULLIF(expr1, expr2)‏ CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END More information on control flow functions can be found here.here

17 Comparison Functions GREATEST(comma separated list of values)‏ LEAST(comma separated list of values)‏

18 Numerical Functions ABS(value)‏ CEIL(value), CEILING(value)‏ COS(radian), SIN(radian), TAN(radian)‏ DEGREES(radian), RADIANS(degree), PI()‏ FLOOR(value)‏ POW(X, Y), SQRT(X)‏ ROUND(value), ROUND(value, decimal_places)‏ TRUNCATE(value, decimal_places)‏

19 Format Versus Truncate FORMAT rounds, TRUNCATE does not SELECT FORMAT(3.14159, 4) Returns 3.1416 SELECT TRUNCATE(3.14159, 4) Returns 3.1415 FORMAT is a string function, and returns a string; TRUNCATE is a mathematical function, and returns a number

20 Numerical Functions More numerical functions can be found here.here

21 Nested Functions Nested functions are very common! String functions CONCAT(LEFT(FirstName, 1), LEFT(LastName, 1)) Date functions DAY(CURDATE()) Control flow functions IF(grade > 90, 'A', IF(grade > 80, 'B', IF(grade > 70, 'C', 'NC')))

22 Functions and Column Aliases Column aliases can be used in any of the following clauses GROUP BY HAVING (MySQL yes, PostgreSQL no) ORDER BY You cannot refer to column aliases in the following clauses SELECT FROM WHERE

23 Functions and Column Aliases Therefore, if you have a long function expression and want to use it in the WHERE clause too, using a column alias won’t work You must manually duplicate the expression (by copying and pasting it) into the WHERE clause

24 Functions and Column Aliases Example: -- ERROR!!! SELECT Title, YEAR(ReleaseDate) AS Y FROM Movies WHERE Y > 2000; -- OK SELECT Title, YEAR(ReleaseDate) AS Y FROM Movies WHERE YEAR(ReleaseDate) > 2000;


Download ppt "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions."

Similar presentations


Ads by Google