COSC 2307: Row Functions
COSC 2307: Row Functions
Module 9
Row Functions
Row Functions
• Row functions calculate a new value based on the data in a single row
of a table
• The value can be based on the date in one column or several different
columns
• Some row functions operate on numbers, and others operate on text
or on dates
Row Functions
• The new values may appear in the results table
• May be used in
• SELECT clause
• WHERE clause
• ORDER BY clause
• The new column of information is not stored on the disk with the
other data of the table
• It does not become a permanent part of the table itself
• It is held in memory while the select statement is being processed
Row Functions
You can create a new table that adds a new column by using a row
function.
• Example in MySQL:
CREATE TABLE product_copy AS
SELECT *, msrp * onhand AS inventory_value
FROM product;
• Example in MS SQL Server
SELECT *, msrp * onhand AS inventory_value
INTO product_copy
FROM product;
Your turn
Create a new table, named customer_new, that adds new column to
the customer table (GeneralStore database). Create new column by
using the following row function:
query 1
2
05/09/1997 Friday
05/09/1997 Friday
3 05/09/1997 Friday
SELECT ordernum, orderdate, 4 01/10/1997 Wednesday
DAYNAME(orderdate) 5 08/10/1997 Wednesday
6 19/10/1997 Sunday
FROM orders ; 7 15/10/1997 Wednesday
8 17/10/1997 Friday
9 04/11/1997 Tuesday
10 06/11/1997 Thursday
11 01/12/1997 Monday
12 04/12/1997 Thursday
13 08/12/1997 Monday
14 15/12/1997 Monday
15 17/12/1997 Wednesday
Formatting String Values in ORACLE
• You can combine data from the tables by concatenating them.
• Besides strings and printable characters you can combine special non-
printable character for formatting purposes:
• CHR(10) – new line
• CHR(13) - carriage return (not used in linux).
• CHR(9) is a tab
•TO_CHAR function
• Converts a DATETIME, number, expression to a TEXT expression in a specified format
•SYSDATE function
• Returns today’s date and time
•ROUND function
SELECT ROUND(123.456, 2) FROM dual;
DATE PART QUERIES in Oracle
SELECT EXTRACT (year FROM orderdate) AS y
FROM s9.tbl_orders;
DATE PART QUERIES in Oracle
SELECT EXTRACT (day FROM orderdate) AS d,
EXTRACT (month FROM orderdate) AS m,
EXTRACT (year FROM orderdate) AS y
FROM s9.tbl_orders;
• The above query pulls out the various parts of the orderdate column in the orders table
• Notice each value is separated and listed as its own column in the resultset
DAYNAME/DATENAME (MySQL/SQL Server)
• If we want to know the day name for a specific date, we can use the
DAYNAME() function in MySQL or DATENAME() in MS SQL Server or
use TO_CHAR(TO_DATE) in Oracle
MySQL:
SELECT DAYNAME('2013-01-23') FROM dual;
MS SQL Server:
SELECT DATENAME(w,'2013-01-23’)
Oracle:
SELECT TO_CHAR(TO_DATE('2013-01-23','yyyy-mm-dd'),'day') AS
dayname FROM dual;
DAYNAME('2013-01-23')
Wednesday
DATENAME (Transact-SQL) – MS SQL Server
• Returns a character string that represents the specified datepart of
the specified date
datepart Abbreviations
Syntax
year yy, yyyy
SELECT DATENAME ( datepart , date ) quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz
ISO_WEEK ISOWK, ISOWW
DAY NAME (MySQL/SQL Server), Oracle
• We might also include DAYNAME (or DATENAME)
as part of a larger query ordernum orderdate DAYNAME(orderdate)
MySQL: 1 05/09/1997Friday
2 05/09/1997 Friday
SELECT ordernum, orderdate, DAYNAME(orderdate) 3 05/09/1997 Friday
FROM orders ; 4 01/10/1997 Wednesday
5 08/10/1997 Wednesday
6 19/10/1997 Sunday
MS SQL Server: 7 15/10/1997 Wednesday
SELECT ordernum, orderdate, 8 17/10/1997 Friday
DATENAME(w,orderdate) 9 04/11/1997 Tuesday
FROM orders ; 10 06/11/1997Thursday
11 01/12/1997 Monday
12 04/12/1997 Thursday
ORACLE: 13 08/12/1997 Monday
SELECT ordernum, 14 15/12/1997 Monday
orderdate,TO_CHAR(TO_DATE(orderdate,'yyyy-mm- 15 17/12/1997 Wednesday
dd'),'day') AS dayname
FROM s9.tbl_orders ;
Display current system date
Using MySQL: Using MS SQL Server:
• CURDATE() • GETDATE()
Will display current system date Will display current system date
Example: Example:
SELECT CURDATE() FROM dual; SELECT GETDATE();
CURDATE()
03/09/2014 2014-09-03 17:46:39.400
Using Oracle:
• Examples:
SELECT * FROM hr.employees WHERE salary > 20000;
Or try:
SELECT * FROM s9.tbl_customer WHERE creditlimit >0;
Other Comparison Operators
• Example:
SELECT *
FROM hr.employees
WHERE salary BETWEEN 15000 AND 20000;
Logical Conditions
• Example:
SELECT *
FROM hr.employees
WHERE salary >10000 AND department_id = 100;
Or try:
SELECT * FROM s9.tbl_customer
WHERE creditlimit >0 AND city LIKE 'Tor%';
Rules of Precedence
Eliminating Duplicate Rows
Or try:
SELECT *
FROM s9.tbl_customer
WHERE creditlimit >0
AND city LIKE 'Tor%'
ORDER BY custname DESC;