Is222 - Concat
Is222 - Concat
1
Concatenation
• CONCAT() returns NULL if any argument is
NULL. The example shows the concatenation
of string literals.
Concatenation
• CONCAT_WS() stands for Concatenate With Separator
and is a special form of CONCAT().
• The first argument is the separator for the rest of the
arguments.
• The separator can be anything like a space, for
instance
• The separator is added between the strings to be
concatenated.
• The separator can be a string, as can the rest of the
arguments.
• If the separator is NULL, the result is NULL.
4
2
Concatenation
• CONCAT_WS() does not skip empty strings.
• However, it does skip any NULL values after the
separator argument.
• MS Access
3
Concat Example - How can I combine the first
name and last name?
• What happens if you use the & or + operator
instead of the concatenate functions in MySql?
• For instance, what would be the result of the
following query in MySql?
– SELECT cus_code, cus_fname & " " &
cus_lname FROM CUSTOMER;
– SELECT cus_code, cus_fname + " " +
cus_lname FROM CUSTOMER;
Aliases (As)
• An alias can be used in a query select list, to
give a column a different name.
• Use the AS operator to create an alias
• You can use the alias in GROUP BY, ORDER
BY, or HAVING clauses to refer to the column:
4
Aliases (as)
• Standard SQL disallows references to column aliases in a
WHERE clause.
• This restriction is imposed because when the WHERE
clause is evaluated, the column value may not yet have
been determined.
• For example, the following query is illegal:
Aliases (as)
• In the select list of a query, a quoted column alias can be
specified using identifier or string quoting characters
– SELECT 1 AS `one`, 2 AS 'two';
• Elsewhere in the statement, quoted references to the alias
must use identifier quoting, or the reference is treated as a
string literal.
• For example, this statement groups by the values in column
id, referenced using the alias `a`:
– SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
GROUP BY `a`;
• But this statement groups by the literal string 'a' and will not
work as expected:
– SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
GROUP BY 'a';
10
5
Aliases example
• https://dev.mysql.com/doc/refman/5.1/en/problems-with-
alias.html (MySql 5.1 reference for As)
• The example below gives the full name generated with concat
using an alias called Name
11
Aliases Exercise 1
• Consider the following columns in a customer table:
• Write a query to show all the details of all the customers from the table
above
• Consider the customer code to be a number
• cus_code should be aliased as Customer Code (two words).
• The first and last names should be concatenated such that they are not
nullified by null entries of the first/last name with the alias FullName.
• Spend about 1-2 minutes on this (answer is on the next slide)
12
• How many solutions are possible?
6
Aliases Exercise 1 - Solution
• Consider the following columns in a customer table:
Arithmetic Operators
• A list of arithmetic operators are given below
7
Using Calculations - Example
• Consider the line table below
• List the invoice number, product code, and the total value of sales of that
product (line qty * line price) - Use arithmetic operators 15
16
8
IFNULL
• Control Flow Functions
• IFNULL(expr1,expr2)
17
IFNULL
• Some examples can be seen as follows:
• There will be a data row with the test field having the value 1
18
9
Special Operators
• A list of different operators is given at the link below:
– https://dev.mysql.com/doc/refman/5.1/en/non-typed-
operators.html
• LIKE
• IS NOT NULL
• IS NULL
• IS NOT
• IS
19
• NOT
20
10
Special Operators – IS NULL
• IS NULL
– Checks whether a particular column is null.
• Example: select all customers who do not have
a middle name
21
11
Special Operators - LIKE
• With LIKE you can use the following two wildcard characters in the
pattern.
23
24
12
Special Operators - LIKE
• Like
– like ‘_m%’ means that the second letter
of the last name should be ‘m’ followed
by anything else
25
13
Special Operators - IN
• expr IN (value list)
Special Operators - IN
• IN
– Allows you to specify a list of values
– Consider the invoice table
28
14
Special Operators - IN
29
15
Joining Database Tables
• The product table has the v_code only
• How can you list the product and the name of the
vendor?
• The name of the vendor is the vendor table
• You need to join the two tables together
• You will do this using the foreign key in the table
design
• You will need a join query to combine the info
from both tables
16
Joining Database Tables
33
• The student and guardian tables are follows (student on the left):
• If we write the query (w/o any filter clause), the results will be:
– select student.stu_id, guardian.guard_id from student, guardian order
by student.stu_id;
34
17
Joining Database Tables
• Now, if we write the query (with some filter clause), the results will
be:
– select student.stu_id, guardian.guard_id from student, guardian where
student.stu_id = "S1" order by student.stu_id ;
• All the rows were filtered to only include records for S1, but still
creating all permutations of S1 with every row in the guardian table
35
36
18
Joining Database Tables
37
• product.v_code = vendor.v_code;
38
19
Joining Database Tables – Exercise 1
• Exercise:
39
40
20
Joining Database Tables – Exercise 2
41
References
• https://dev.mysql.com/doc/refman/5.1/en/contro
l-flow-functions.html#function_ifnull
• http://www.tutorialspoint.com/mysql/mysql-
between-clause.htm
• https://dev.mysql.com/doc/refman/5.0/en/string-
comparison-functions.html#operator_like
• https://dev.mysql.com/doc/refman/5.1/en/comp
arison-operators.html#function_in
42
21