Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010.

Similar presentations


Presentation on theme: "Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010."— Presentation transcript:

1 Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010

2 LOWER() and UPPER()  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 Nasional2 Syntax LOWER(character_expression) UPPER(character_expression)

3 LOWER() and UPPER() – cont. College of Information Technology, Universiti Tenaga Nasional 3 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

4 ROUND()  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 Nasional4 Syntax ROUND(numeric_expression, length)

5 ROUND() – cont. Rounding up the value 457.923 to varying precisions: PrecisionResult -2 Round(457.923, -2)  500.000 Round(457.923, -1)  460.000 0 Round(457.923, 0)  458.00 1 Round(457.923, 1)  457.900 2 Round(457.923, 2)  457.920 College of Information Technology, Universiti Tenaga Nasional5

6 ROUND() – cont. College of Information Technology, Universiti Tenaga Nasional 6 Example 2 Round up 457.923 to the hundreds. SELECT ROUND(457.923, -2) Example 3 th Round up 457.923 to the hundredths, or to two decimal places. SELECT ROUND(457.923, 2)

7 GETDATE() system  Returns the current system date and time  Accepts no argument College of Information Technology, Universiti Tenaga Nasional7 Syntax GETDATE()

8 DATEADD()  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 Nasional8 Syntax DATEADD(date_part, numeric_expression, date_expression)

9 DATEADD() – cont. College of Information Technology, Universiti Tenaga Nasional 9 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.

10 DATEDIFF()  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 Nasional10 Syntax DATEDIFF(date_part, date_expression, date_expression)

11 DateDiff() – cont. College of Information Technology, Universiti Tenaga Nasional 11 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!

12 DATENAME()  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 Nasional12 Syntax DATENAME(date_part, date_expression)

13 DateName() – cont. College of Information Technology, Universiti Tenaga Nasional 13 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.

14 Convert()  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 Nasional14 Syntax Convert(data_type[(length)], expression[, style])

15 Convert() – cont. College of Information Technology, Universiti Tenaga Nasional 15 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

16 Group Functions College of Information Technology, Universiti Tenaga Nasional 16 FunctionDescription 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.

17 MAX and MIN  Example 8 Display the minimum and maximum salary earn by the employee. SELECT MIN(salary), MAX(salary) FROMEmp College of Information Technology, Universiti Tenaga Nasional 17

18 COUNT  Example 9 Display the number of employees in Sales department SELECTCOUNT(*) FROMemp WHEREdept = ‘sales’ College of Information Technology, Universiti Tenaga Nasional 18

19 Group Functions College of Information Technology, Universiti Tenaga Nasional 19 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

20 Group Functions-cont.  Example 1 Display the number of employee for each department. Name the column as ‘No of employee’ SELECTDept, COUNT(*) ‘No of employee’ FROMEmp GROUP BYDept College of Information Technology, Universiti Tenaga Nasional 20

21 WHERE or HAVING? College of Information Technology, Universiti Tenaga Nasional 21 SELECTDept, AVG(salary) FROMEmp WHEREAVG(salary) > 1500 GROUP BY Dept SELECTDept, AVG(salary) FROMEmp GROUP BY Dept HAVING AVG(salary) > 1500


Download ppt "Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010."

Similar presentations


Ads by Google