Download presentation
Presentation is loading. Please wait.
Published byOliver Stewart Modified over 9 years ago
1
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009
2
College of Information Technology, Universiti Tenaga Nasional2 ROUND() Returns a numeric expression, rounded to the specified length or precision Returns a numeric expression, rounded to the specified length or precision Accepts two arguments: Accepts two arguments: numeric_expression - value to be rounded up numeric_expression - value to be rounded up length - precision to which the first argument is to be rounded, must be an integer length - precision to which the first argument is to be rounded, must be an integer Syntax ROUND(numeric_expression, length)
3
College of Information Technology, Universiti Tenaga Nasional3 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
4
College of Information Technology, Universiti Tenaga Nasional4 ROUND() – cont. Example 1 Round up 457.923 to the hundreds. SELECT ROUND(457.923, -2) Example 2 Round up 457.923 to the hundredths, or to two decimal places. SELECT ROUND(457.923, 2)
5
College of Information Technology, Universiti Tenaga Nasional5 LOWER() and UPPER() Returns a character expression, in lower-case or upper-case Returns a character expression, in lower-case or upper-case The argument may be a: The argument may be a: Table column Table column String String Concatenation/combination of the above Concatenation/combination of the above Syntax LOWER(character_expression) UPPER(character_expression)
6
College of Information Technology, Universiti Tenaga Nasional6 LOWER() and UPPER() – cont. Example 3 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
7
College of Information Technology, Universiti Tenaga Nasional7 GETDATE() Returns the current system date and time Returns the current system date and time Accepts no argument Accepts no argument Syntax GETDATE()
8
College of Information Technology, Universiti Tenaga Nasional8 DATEADD() Returns a datetime value, the result of adding a specified length of time to a specified start date Returns a datetime value, the result of adding a specified length of time to a specified start date Accepts three arguments: Accepts three arguments: date_part – Measure of time to add e.g. Month date_part – Measure of time to add e.g. Month numeric_expression – Multiple of the measure, to get a length of time e.g. 6 6 months numeric_expression – Multiple of the measure, to get a length of time e.g. 6 6 months date_expression – Start date date_expression – Start date Syntax DATEADD(date_part, numeric_expression, date_expression)
9
College of Information Technology, Universiti Tenaga Nasional9 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.
10
College of Information Technology, Universiti Tenaga Nasional10 DATEDIFF() Returns the length of time between two dates, in a specified measure of time Returns the length of time between two dates, in a specified measure of time Accepts three arguments: Accepts three arguments: date_part – Measure of time to use e.g. Week, Day date_part – Measure of time to use e.g. Week, Day date_expression – Start date date_expression – Start date date_expression – End date date_expression – End date Syntax DATEDIFF(date_part, date_expression, date_expression)
11
College of Information Technology, Universiti Tenaga Nasional11 DateDiff() – cont. Example 5 How many days more to the National Day? SELECT DateDiff(Day, ‘8 August, 2007’, ’31 August, 2007’) Date string values are also enclosed in single quotes!
12
College of Information Technology, Universiti Tenaga Nasional12 DATENAME() Returns a character string representing the specified date part of the specified date Returns a character string representing the specified date part of the specified date Accepts two arguments: Accepts two arguments: date_part – Part of the date to be returned date_part – Part of the date to be returned date_expression – Date to be used date_expression – Date to be used Syntax DATENAME(date_part, date_expression)
13
College of Information Technology, Universiti Tenaga Nasional13 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.
14
College of Information Technology, Universiti Tenaga Nasional14 Convert() Converts an expression from one data type to another Converts an expression from one data type to another Accepts three arguments Accepts three arguments data_type – data type to convert into data_type – data type to convert into expression – expression to be converted expression – expression to be converted style – format of the returned expression (refer to the Transact-SQL Help) style – format of the returned expression (refer to the Transact-SQL Help) Syntax Convert(data_type[(length)], expression[, style])
15
College of Information Technology, Universiti Tenaga Nasional15 Convert() – cont. Example 7 Display the employees’ hire dates in British format i.e. dd/mm/yy. SELECT LastName, Convert(char, HireDate, 3) AS ‘Hire Date’ FROM Employees
16
College of Information Technology, Universiti Tenaga Nasional16 Convert() – cont. Example 8 Display all products and their prices in this format: nnn,nnn.nn. SELECT ProductName, Convert(char(10), UnitPrice, 1) AS ‘Unit Price’ FROM Products
17
College of Information Technology, Universiti Tenaga Nasional17 The LIKE Logical Operator Logical Operators LIKE Matches the string expression LastName LIKE ‘Bu%’ NOT LIKE Does not match the string expression ProductName NOT LIKE ‘_e%’
18
College of Information Technology, Universiti Tenaga Nasional18 The LIKE Logical Operator – cont. Wildcard Characters % Represents any number of characters _(Underscore) Represents exactly one character
19
College of Information Technology, Universiti Tenaga Nasional19 The LIKE Logical Operator – cont. Example 9 Display the details of the employee named Buchanan. But… you don’t know how to spell Buchanan spell Buchanan SELECT * FROM Employees WHERE LastName LIKE ‘Bu%’
20
College of Information Technology, Universiti Tenaga Nasional20 The LIKE Logical Operator – cont. Example 10 Display all products that do not have the letter ‘e’ as the second letter in their names. SELECT ProductName FROM Products WHERE ProductName NOT LIKE ‘_e%’
21
College of Information Technology, Universiti Tenaga Nasional21 The LIKE Logical Operator – cont. Example 11 Display all employees who were hired in 1995. SELECT LastName, HireDate FROM Employees WHERE HireDate LIKE ‘%95%’
22
College of Information Technology, Universiti Tenaga Nasional22 The LIKE Logical Operator – cont. Challenge yourself! Find other ways of obtaining the same results as Example 11. Use other operators in the condition. SELECT LastName, HireDate FROM Employees WHERE ???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.