Download presentation
Presentation is loading. Please wait.
1
Lab 3: Single-row Functions
CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional
2
LOWER() and UPPER() Syntax LOWER(character_expression) UPPER(character_expression) Returns a character expression, in lower-case or upper-case The argument may be a: Table column String Concatenation/combination of the above College of Information Technology, Universiti Tenaga Nasional
3
LOWER() and UPPER() – cont.
Example 1 Display employees’ names and job titles. Display the names in this format ‘LASTNAME, FirstName’ and give the column a suitable heading. SELECT UPPER(LastName) + ‘, ‘ + FirstName AS Name, Title AS ‘Job Title’ FROM Employees College of Information Technology, Universiti Tenaga Nasional
4
ROUND() Syntax ROUND(numeric_expression, length) Returns a numeric expression, rounded to the specified length or precision Accepts two arguments: numeric_expression - value to be rounded up length - precision to which the first argument is to be rounded, must be an integer College of Information Technology, Universiti Tenaga Nasional
5
ROUND() – cont. Rounding up the value 457.923 to varying precisions:
Result -2 Round( , -2) -1 Round( , -1) Round( , 0) 1 Round( , 1) 2 Round( , 2) College of Information Technology, Universiti Tenaga Nasional
6
ROUND() – cont. Example 2 Round up 457.923 to the hundreds. Example 3
SELECT ROUND( , -2) Example 3 Round up to the hundredths, or to two decimal places. SELECT ROUND( , 2) College of Information Technology, Universiti Tenaga Nasional
7
GETDATE() Returns the current system date and time Accepts no argument
Syntax GETDATE() Returns the current system date and time Accepts no argument College of Information Technology, Universiti Tenaga Nasional
8
DATEADD() Syntax DATEADD(date_part, numeric_expression, date_expression) Returns a datetime value, the result of adding a specified length of time to a specified start date Accepts three arguments: date_part – Measure of time to add e.g. Day, Month, Year numeric_expression – Multiple of the measure, to get a length of time e.g. 6 6 months date_expression – Start date College of Information Technology, Universiti Tenaga Nasional
9
DATEADD() – cont. Example 4 What date is 6 months from today?
SELECT GetDate() AS Today, DateAdd(Month, 6, GetDate()) AS ‘6 Months from Today’ Functions can be nested! Here, GetDATE() is nested inside DateADD() and used as the start date argument. College of Information Technology, Universiti Tenaga Nasional
10
DATEDIFF() Syntax DATEDIFF(date_part, date_expression, date_expression) Returns the length of time between two dates, in a specified measure of time Accepts three arguments: date_part – Measure of time to use e.g. Week, Day date_expression – Start date date_expression – End date College of Information Technology, Universiti Tenaga Nasional
11
DateDiff() – cont. Example 5 How many days more to the National Day?
SELECT DateDiff(Day, ‘30 July, 2009’, ’31 August, 2009’) Date string values are also enclosed in single quotes! College of Information Technology, Universiti Tenaga Nasional
12
DATENAME() Syntax DATENAME(date_part, date_expression) Returns a character string representing the specified date part of the specified date Accepts two arguments: date_part – Part of the date to be returned date_expression – Date to be used College of Information Technology, Universiti Tenaga Nasional
13
DateName() – cont. Example 6 What month is it now?
SELECT DateName(Month, ‘8 August, 2007’) Some other ways of writing date constants are: 'April 15, 1998' '980415' '04/15/98‘ Look up ‘Constants’ in Transact-SQL Help. College of Information Technology, Universiti Tenaga Nasional
14
Convert() Converts an expression from one data type to another
Syntax Convert(data_type[(length)], expression[, style]) Converts an expression from one data type to another Accepts three arguments data_type – data type to convert into expression – expression to be converted style – format of the returned expression (refer to the Transact-SQL Help) College of Information Technology, Universiti Tenaga Nasional
15
Convert() – cont. Example 7
Display the employees’ start dates in British format i.e. dd/mm/yy. SELECT Name, Convert(char, StartDate, 3) AS ‘Hire Date’ FROM emp College of Information Technology, Universiti Tenaga Nasional
16
Group Functions Function Description MAX(DISTINCT | ALL | expr)
Maximum value. MIN(DISTINCT | ALL | expr) Minimum value. AVG(ALL | n) Average value of n, ignoring null values. SUM(DISTINCT | ALL | n) Sum values of n, ignoring null values. COUNT(*) Returns the number of rows affected. COUNT(DISTINCT | ALL | expr) Returns the number of rows affected by expr. College of Information Technology, Universiti Tenaga Nasional
17
MAX and MIN Example 8 Display the minimum and maximum salary earn by the employee. SELECT MIN(salary), MAX(salary) FROM Emp College of Information Technology, Universiti Tenaga Nasional
18
COUNT Example 9 Display the number of employees in Sales department
SELECT COUNT(*) FROM emp WHERE dept = ‘sales’ College of Information Technology, Universiti Tenaga Nasional
19
Group Functions Syntax SELECT select_list FROM table_source [WHERE search_condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY order_by_expression] group_by_expression: Specifies columns whose values determine the grouping rows group_condition: Restricts the groups of rows returned College of Information Technology, Universiti Tenaga Nasional
20
Group Functions-cont. Example 1
Display the number of employee for each department. Name the column as ‘No of employee’ SELECT Dept, COUNT(*) ‘No of employee’ FROM Emp GROUP BY Dept College of Information Technology, Universiti Tenaga Nasional
21
WHERE or HAVING? SELECT Dept, AVG(salary) FROM Emp WHERE AVG(salary) > 1500 GROUP BY Dept SELECT Dept, AVG(salary) FROM Emp GROUP BY Dept HAVING AVG(salary) > 1500 College of Information Technology, Universiti Tenaga Nasional
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.