Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Similar presentations


Presentation on theme: "Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:"— Presentation transcript:

1 Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275: Database Design (Fall 2015)

2 2 Quick Reminder: If you need to email me about anything, please do so using my cduckett@cascadia.edu account which I check several times a day. Do not use or reply to the clduckett@gmail.com account which I use only for returning files from StudentTracker and only check once or twice a month! cduckett@cascadia.edu  NO CLASS NEXT TUESDAY, October 27 th. It is a non-instructional day. MID-TERM EXAM is LECTURE 10, Tuesday, November 3 rd Assignment 2 is due LECTURE 11, Thursday, November 5 th, in StudentTracker by MIDNIGHT It will cover everything discussed through Thursday’s lecture, Lecture 9, including any chapter reading from The DATABASE DESIGN FOR MERE MORTALS and THE LANGUAGE OF SQL books. You’re allowed to use one large index card for crib notes during the mid-term. I’ll post a Mid-Term Study Guide on the BIT275 website sometime on Friday, May 8.

3 3 Tuesday (LECTURE 8) Database Design for Mere Mortals: Chapter 6 Thursday (LECTURE 9) The Language of SQL: Chapters 7, 8

4 4 Patterns Order of Evaluations Concatenation Substring TRIM POSITION

5 5 Patterns http://www.w3schools.com/sql/sql_like.asp http://www.w3schools.com/sql/sql_in.asp http://www.w3schools.com/sql/sql_between.asp http://www.w3schools.com/sql/sql_and_or.asp https://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html http://www.w3schools.com/sql/sql_wildcards.asp

6 Matching Patterns with LIKE You can use LIKE to retrieve rows based on partial information. LIKE is useful if you don’t know an exact value (“The author’s last name is Kel-something”) or you want to retrieve rows with similar values (“Which authors live in the San Francisco Bay Area?”). The LIKE condition’s important characteristics are: LIKE works with only character strings, not numbers or datetimes. LIKE uses a pattern that values are matched against. A pattern is a quoted string that contains the literal characters to match and any combination of wildcards. Wildcards are special characters used to match parts of a value. Wildcard Operators

7 String comparisons are case insensitive or case sensitive, depending on your DBMS. You can negate a LIKE condition with NOT LIKE. You can combine LIKE conditions and other conditions with AND and OR.

8

9 Escape and Unescaped Patterns

10 [ ] and [ ^ ] Patterns

11 BETWEEN Use BETWEEN to determine whether a given value falls within a specified range. The BETWEEN condition’s important characteristics are: BETWEEN works with character strings, numbers, and datetimes. The BETWEEN range contains a low value and a high value, separated by AND. The low value must be less than or equal to the high value. BETWEEN is a convenient, shorthand clause that you can replicate by using AND. BETWEEN is equivalent to: WHERE (test_column >= low_value) AND (test_column <= high_value) BETWEEN specifies an inclusive range, in which the high value and low value are included in the search. To specify an exclusive range, which excludes endpoints, use > and comparisons instead of BETWEEN: WHERE (test_column > low_value) AND (test_column < high_value) String comparisons are case insensitive or case sensitive. You can negate a BETWEEN condition with NOT BETWEEN. You can combine BETWEEN conditions and other conditions with AND and OR.

12 BETWEEN

13 IN Use IN to determine whether a given value matches any value in a specified list. The IN condition’s important characteristics are: IN works with character strings, numbers, and datetimes. The IN list is a parenthesized listing of one or more comma-separated values. The list items needn’t be in any particular order. IN is a convenient, shorthand clause that you can replicate by using OR. is equivalent to:

14 14 Order of Evaluations Operator Precedence https://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

15 Operators and Order of Evaluation 1 + 2 * 3 + 4 * 2 + 4 * 3 – 2 = ? 1 + (2 * 3) + (4 * 2) + (4 * 3) – 2 = 1 + (6 + 8 + 12) – 2 = (1 + 26) – 2 = 27 – 2 = 5

16 16 Concatenation

17 Use the operator || to combine, or concatenate, strings. The operator’s important characteristics are: The operator || is two consecutive vertical-bar, or pipe, characters Concatenation doesn’t add a space between strings. || combines two strings into a single string: 'formal' || 'dehyde' is 'formaldehyde‘ You can chain concatenations to combine multiple strings into a single string: 'a' || 'b' || 'c' || 'd' is 'abcd‘ Concatenation with an empty string ('') leaves a string unchanged: 'a' || ' ' || 'b' is 'ab‘ The result of any concatenation operation that involves a null is null: 'a' || NULL || 'b' is NULL To concatenate a string and a nonstring (such as a numeric or datetime value), you must convert the nonstring to a string if your DBMS doesn’t convert it implicitly (CAST). NOTE : MySQL uses the CONCAT() function for concatenation http://www.mysqltutorial.org/sql-concat-in-mysql.aspx http://www.techonthenet.com/mysql/functions/concat.php http://www.electrictoolbox.com/mysql-string-concatenation/ http://www.electrictoolbox.com/mysql-string-concatenation-part2/ (CONCAT_WS) http://www.electrictoolbox.com/mysql-string-concatenation-part2/ Concatenation with separator

18 Concatenation

19 19 Substring http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php http://www.techonthenet.com/mysql/functions/substring.php http://www.mysqltutorial.org/mysql-substring.aspx

20 SUBSTRING Use the function SUBSTRING() to extract part of a string. The function’s important characteristics are: A substring is any sequence of contiguous characters from the source string, including an empty string or the entire source string itself. SUBSTRING() extracts part of a string starting at a specified position and continuing for specified number of characters. A substring of an empty string is an empty string. If any argument is null, SUBSTRING() returns null. SUBSTRING(string FROM start [FOR length]) string is the source string from which to extract the substring. string is a string expression such as a column that contains character strings, a string literal, or the result of an operation or function that returns a string. start is an integer that specifies where the substring begins. length is an integer that specifies the length of the substring (the number of characters to return). If FOR length is omitted, SUBSTRING() returns all the characters from start to the end of string

21 SUBSTRING

22 Changing String Case Use the function UPPER() to return a string with lowercase letters converted to uppercase, and use the function LOWER() to return a string with uppercase letters converted to lowercase. The functions’ important characteristics are: A cased character is a letter that can be lowercase (a) or uppercase (A). Case changes affect only letters. Digits, punctuation, and whitespace are left unchanged. Case changes have no effect on empty strings (''). If its argument is null, UPPER() and LOWER() return null.

23 Changing String Case EXAMPLE OF WILDCARD WITH STRING CASE: List the titles that contain the characters MO, regardless of case. All the letters in the LIKE pattern must be uppercase for this query to work.

24 24 TRIM Function http://www.w3resource.com/mysql/string-functions/mysql-trim-function.php

25 TRIM Use the function TRIM() to remove unwanted characters from the ends of a string. The function’s important characteristics are: You can trim leading characters, trailing characters, or both. (You can not use TRIM() to remove characters from within a string.) By default, TRIM() trims spaces, but you can strip off any unwanted characters, such a leading and trailing zeros or asterisks. TRIM() typically is used to format results and make comparisons in a WHERE clause. TRIM() is useful for trimming trailing spaces from CHAR values. Trimming has no effect on empty strings (''). If any argument is null, TRIM() returns null. To trim spaces from a string: TRIM([ [LEADING | TRAILING | BOTH] FROM] string) string is a string expression such as a column that contains character strings, a string literal, or the result of an operation or function that returns a string. Specify LEADING to remove leading spaces, TRAILING to remove trailing spaces, or BOTH to remove leading and trailing spaces. If this specifier is omitted, BOTH is assumed.

26 TRIM

27 27 POSITION Function http://www.w3resource.com/mysql/string-functions/mysql-position-function.php

28 POSITION Use the function POSITION() to locate a particular substring within a given string. The function’s important characteristics are: POSITION() returns an integer (=0) that indicates the starting position of a substring’s first occurrence within a string. If the string doesn’t contain the substring, POSITION() returns zero. String comparisons are case insensitive or case sensitive, depending on your DBMS The position of any substring within an empty string ('') is zero. If any argument is null, POSITION() returns null. POSITION (substring IN string)

29 POSITION

30 30 BIT 275 ICE 8


Download ppt "Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:"

Similar presentations


Ads by Google