Using Built-In Functions

Slides:



Advertisements
Similar presentations
Module 12: Auditing SQL Server Environments
Advertisements

Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Databases Week 1, lab 2 Simple selects. About the environment We are using SQL Server for the moment. The server we are using is: –Cian.student.comp.dit.ie.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Introduction to Databases Chapter 6: Understanding the SQL Language.
Functions Lesson 10. Skills Matrix Function A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The.
Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 2: Querying and Filtering Data. Using the SELECT Statement Filtering Data Working with NULL Values Formatting Result Sets Performance Considerations.
Querying with Transact-SQL
Creating Database Objects
Retrieving Data Using the SQL SELECT Statement
Creating Oracle Business Intelligence Interactive Dashboards
In this session, you will learn to:
Using DML to Modify Data
Introduction to Microsoft SQL Server 2016
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Introduction to T-SQL Querying
02 | Advanced SELECT Statements
Sorting and Filtering Data
Querying Multiple Tables
Deploying and Configuring SSIS Packages
Module 11: Introduction to DAX Module 11 Introduction to DAX
20761A 10: Using Subqueries Module 10   Using Subqueries.
05 | Using Functions and Aggregating Data
20761A 11: Using Set Operators Module 11   Using Set Operators.
Automating SQL Server Management
Using Window Ranking, Offset, and Aggregate Functions
MATLAB: Structures and File I/O
14 T-SQL Functions You May Not Know
Sorting and Filtering Data
Module 1 Introduction to Microsoft SQL Server 2016
(SQL) Aggregating Data Using Group Functions
20761B 12: Using Set Operators Module 12   Using Set Operators.
Using the Set Operators
Writing SELECT Queries
20761B 10: Using Subqueries Module 10   Using Subqueries.
Module 13: Creating Data Visualizations with Power View
Adding Calculations to a Fact
Module 12: Implementing an Analysis Services Tabular Data Model
Using Table Expressions
Module 7: Working with Measures and Measure Groups
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Access Tutorial 5 Creating Advanced Queries and Enhancing Table Design
Creating Noninput Items
14 T-SQL Functions You May Not Know
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Objectives In this lesson, you will learn to:
Using DML to Modify Data
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Creating Database Objects
Aggregating Data Using Group Functions
Introduction to SQL Server and the Structure Query Language
Module 1 Introduction to Microsoft SQL Server 2017
Designing and Implementing User- Defined Functions
Grouping and Aggregating Data
Module 8: Introduction to MDX Module 8 Introduction to MDX
Designing and Implementing Stored Procedures
Presentation transcript:

Using Built-In Functions Module 8   Using Built-In Functions

Using Functions to Work with NULL 20761B Module Overview 8: Using Built-In Functions Using Functions to Work with NULL  

Lesson 1: Writing Queries with Built-In Functions 8: Using Built-In Functions Demonstration: Writing Queries Using Built-in Functions  

SQL Server Built-in Function Types 8: Using Built-In Functions SQL Server functions can be categorized by scope of input and type of output: Notes: This course will cover aggregates and window functions in later modules. Rowset functions are beyond the scope of this course. The rest of this module will cover various scalar functions. Function Category Description Scalar Operate on a single row, return a single value Grouped Aggregate Take one or more values but return a single summarizing value Window Operate on a window (set) of rows Rowset Return a virtual table that can be used in a T-SQL statement

Return a single (scalar) value 20761B Scalar Functions 8: Using Built-In Functions Operate on elements from a single row as inputs, return a single value as output Return a single (scalar) value Can be used like an expression in queries May be deterministic or nondeterministic Collation depends on input value or default collation of database Scalar Function Categories Configuration Conversion Cursor Date and Time Logical Mathematical Metadata Security String System System Statistical Text and Image The CAST and ABS examples need no database context as written. (Note that CAST will be covered in Lesson 2 of this module.) Scalar functions can have multiple inputs—for example, DATEADD. They can also have no inputs—for example, GETDATE. Only some common scalar functions will be covered in this course. Note: Briefly define deterministic (always return the same result any time they are called, by using a specific set of input values) versus nondeterministic (could return different results every time they are called, even with the same specific set of input values). For more information on determinism, consult your local philosophy professor, clergy or see the SQL Server 2016 Technical Documentation: Deterministic and Nondeterministic Functions http://go.microsoft.com/fwlink/?LinkID=402745

Functions that operate on sets, or rows, of data Summarize input rows 20761B Aggregate Functions 8: Using Built-In Functions Functions that operate on sets, or rows, of data Summarize input rows Without GROUP BY clause, all rows are arranged as one group Will be covered later in the course Note: Aggregate functions will be covered in a later module, along with the GROUP BY clause. Note: SQL Server 2012 introduced new analytic functions that compute an aggregate value based on a window of rows. Unlike grouped aggregate functions, analytic functions can return multiple rows for each group. You can use analytic functions to compute moving averages, running totals, percentages, or top-N results within a group. SELECT COUNT(*) AS numorderlines, SUM(qty*unitprice) AS totalsales FROM Sales.OrderDetails; numorderlines totalsales ------------- ---------- 2155 56500.91

Functions applied to a window, or set of rows 20761B Window Functions 8: Using Built-In Functions Functions applied to a window, or set of rows Include ranking, offset, aggregate, and distribution functions Will be covered later in the course This example calculates a ranking based on the unitprice, with the highest price ranked at 1, the next highest ranked 2, and so on. Tell the students that this is provided for illustration only. The OVER clause and ranking functions will be covered in a later module. SELECT TOP(5) productid, productname, unitprice, RANK() OVER(ORDER BY unitprice DESC) AS rankbyprice FROM Production.Products ORDER BY rankbyprice; productid productname unitprice rankbyprice --------- ------------- --------- ----------- 8 Product QDOMO 263.50 1 29 Product VJXYN 123.79 2 9 Product AOZBW 97.00 3 20 Product QHFFP 81.00 4 18 Product CKEDC 62.50 5

Return an object that can be used like a table in a T-SQL statement Rowset Functions 8: Using Built-In Functions Return an object that can be used like a table in a T-SQL statement Include OPENDATASOURCE, OPENQUERY, OPENROWSET, and OPENXML Beyond the scope of this course  

Demonstration: Writing Queries Using Built-in Functions In this demonstration, you will see how to: Use built-in scalar functions Preparation Steps Start the 20761B-MIA-DC and 20761B-MIA-SQL virtual machines. Demonstration Steps Use Built-in Scalar Functions Ensure that the 20761B-MIA-DC and 20761B-MIA-SQL virtual machines are both running, and then log on to 20761B-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. Run D:\Demofiles\Mod08\Setup.cmd as an administrator. In the User Account Control dialog box, click Yes. At the command prompt, type y, and then press Enter. When the script has finished, press Enter. Start SQL Server Management Studio and connect to the MIA-SQL database engine instance using Windows authentication. Open the Demo.ssmssln solution in the D:\Demofiles\Mod08\Demo folder. In Solution Explorer, expand Queries, and then double-click 11 - Demonstration A.sql. Select the code under the comment Step 1, and then click Execute. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Select the code under the comment Step 4, and then click Execute. Keep SQL Server Management Studio open for the next demonstration.

Lesson 2: Using Conversion Functions 20761B Lesson 2: Using Conversion Functions 8: Using Built-In Functions Demonstration: Using Conversion Functions Question You are writing a query against a Human Resources database. You want to ensure that the Employee.StartDate values are displayed in standard British form. What function should you use? Answer PARSE() or TRY_PARSE.

Implicit and Explicit Data Type Conversions 20761B Implicit and Explicit Data Type Conversions 8: Using Built-In Functions Implicit conversion occurs automatically and follows data type precedence rules Use explicit conversion: When implicit would fail or is not permitted To override data type precedence Explicitly convert between types with CAST or CONVERT functions Watch for truncation  

Converts a value from one data type to another: 20761B Converting with CAST 8: Using Built-In Functions Converts a value from one data type to another: Can be used in SELECT and WHERE clauses ANSI standard CAST syntax: CAST example: Returns an error if data types are incompatible: Note: The use of conversion functions in the WHERE clause may prevent the query optimizer from considering indexes, and may degrade performance. Recommend to the students that they use CAST, as it is standards-based (and has simpler syntax). CAST(<value> AS <datatype>) SELECT CAST(SYSDATETIME() AS date); --attempt to convert datetime2 to int SELECT CAST(SYSDATETIME() AS int); Msg 529, Level 16, State 2, Line 1 Explicit conversion from data type datetime2 to int is not allowed.

Converting with CONVERT 20761B Converting with CONVERT 8: Using Built-In Functions Converts a value from one data type to another: Can be used in SELECT and WHERE clauses CONVERT is specific to SQL Server, not standards-based Style specifies how input value is converted: Date, time, numeric, XML, and so on Syntax: Example: The style code of 112 used in the example specifies the yyyymmdd ISO standard, including century. A style of 12 would return yymmdd. CONVERT (<datatype>, <value>, <optional style no.>) CONVERT(CHAR(8), CURRENT_TIMESTAMP,112) AS ISO_style; ISO_style --------- 20120212

Converting Strings with PARSE 20761B Converting Strings with PARSE 8: Using Built-In Functions PARSE converts strings to date, time, and number types: PARSE example: Note: The USING clause for a culture is optional and, if omitted, the current session’s language settings will be used. PARSE element Comment String_value Formatted nvarchar(4000) input Data_type Requested data type ouput Culture Optional string in .NET culture form: en-US, es-ES, ar-SA, and so on SELECT PARSE('02/12/2012' AS datetime2 USING 'en-US') AS parse_result;

Converting with TRY_PARSE and TRY_CONVERT 20761B Converting with TRY_PARSE and TRY_CONVERT 8: Using Built-In Functions TRY_PARSE and TRY_CONVERT: Return the results of a data type conversion: Like PARSE and CONVERT, they convert strings to date, time and numeric types Unlike PARSE and CONVERT, they return a NULL if the conversion fails TRY_PARSE Example:   SELECT TRY_PARSE(‘SQLServer' AS datetime2 USING 'en-US') AS try_parse_result; try_parse_result ---------------- NULL

Demonstration: Using Conversion Functions 20761B Demonstration: Using Conversion Functions 8: Using Built-In Functions In this demonstration, you will see how to: Use functions to convert data Preparation Steps Complete the previous demonstration in this module. Demonstration Steps Use Functions to Convert Data In Solution Explorer, open the 21 - Demonstration B.sql script file. Select the code under the comment Step 1, and then click Execute. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Note the error message. Select the code under the comment Step 4a, and then click Execute. Select the code under the comment Step 4b, and then click Execute. Note the error message. Select the code under the comment Step 5, and then click Execute. Select the code under the comment Step 6, and then click Execute. Select the code under the comment Step 7, and then click Execute. Select the code under the comment Step 8a, and then click Execute. Note the error message. Select the code under the comment Step 8b, and then click Execute. Keep SQL Server Management Studio open for the next demonstration.

Lesson 3: Using Logical Functions 20761B Lesson 3: Using Logical Functions 8: Using Built-In Functions Demonstration: Using Logical Functions Question You have the following query: SELECT e.FirstName, e.LastName, e.FirstAider FROM Employees AS e The FirstAider column contains ones and zeros. How can you change the query to make the results more readable? Answer Use an IIF function: SELECT e.FirstName, e.LastName, IIF(e.FirstAider = 1, 'Can administer First Aid','No First Aid Skills') This will output descriptive text instead of ones and zeros.

Writing Logical Test with Functions 20761B Writing Logical Test with Functions 8: Using Built-In Functions ISNUMERIC tests whether an input expression is a valid numeric data type: Returns a 1 when the input evaluates to any valid numeric type, including FLOAT and MONEY Returns 0 otherwise Example: Question: How might you use ISNUMERIC when testing data quality? The ideal answer for this should be in the instructor notes. Answers will vary, but might include logic to replace invalid data with a substitute. The demonstration for this lesson contains code similar to the code on the slide. SELECT ISNUMERIC('SQL') AS isnmumeric_result; isnmumeric_result ----------------- SELECT ISNUMERIC(‘101.99') AS isnmumeric_result; isnmumeric_result ----------------- 1

Performing Conditional Tests with IIF 20761B Performing Conditional Tests with IIF 8: Using Built-In Functions IIF returns one of two values, depending on a logical test Shorthand for a two-outcome CASE expression: IIF example: Note: This function, like CHOOSE in the next topic, is designed to support migration from Access and similar environments. The demonstration for this lesson contains code similar to the code on the slide. IIF Element Comments Boolean_expression Logical test evaluating to TRUE, FALSE, or UNKNOWN True_value Value returned if expression evaluates to TRUE False_value Value returned if expression evaluates to FALSE or UNKNOWN SELECT productid, unitprice, IIF(unitprice > 50, 'high','low') AS pricepoint FROM Production.Products;

Selecting Items from a List with CHOOSE 20761B Selecting Items from a List with CHOOSE 8: Using Built-In Functions CHOOSE returns an item from a list as specified by an index value: CHOOSE example: CHOOSE returns the item at the specified index from a list of values. The demonstration for this lesson contains code similar to the code on the slide. CHOOSE Element Comments Index Integer that represents position in list Value_list List of values of any data type to be returned SELECT CHOOSE (3, 'Beverages', 'Condiments', 'Confections') AS choose_result; choose_result ------------- Confections

Demonstration: Using Logical Functions 20761B Demonstration: Using Logical Functions 8: Using Built-In Functions In this demonstration, you will see how to: Use logical functions Preparation Steps Complete the previous demonstration in this module. Demonstration Steps Using Logical Functions In Solution Explorer, open the 31 - Demonstration C.sql script file. Select the code under the comment Step 1, and then click Execute. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Select the code under the comment Step 4, and then click Execute. Select the code under the comment Step 5, and then click Execute. Keep SQL Server Management Studio open for the next demonstration.

Lesson 4: Using Functions to Work with NULL 20761B Lesson 4: Using Functions to Work with NULL 8: Using Built-In Functions Demonstration: Using Functions to Work with NULL Question You are writing a query against the Employees table in the Human Resources database. The CurrentStatus column can contain the string values “New”, “Retired”, and “Under Caution”. Many employees have this column set to NULL when those statuses do not apply to them. For confidentiality, you want to ensure that the employees currently under caution are displayed like those employees with no applicable status. What function should you use? ( )Option 1: ISNULL() ( )Option 2: COALESCE() ( )Option 3: NULLIF() ( )Option 4: TRY_PARSE() ( )Option 5: PARSE() Answer (√) Option -2: NULLIF()

Converting NULL with ISNULL 20761B Converting NULL with ISNULL 8: Using Built-In Functions ISNULL replaces NULL with a specified value Not standard; use COALESCE instead Syntax: ISNULL example: ISNULL Element Comment expression_to_check Return expression itself if not NULL replacement_value Returned if expression evaluates to NULL Note: Note: If necessary, the replacement value is implicitly converted to the data type of the expression checked. SELECT custid, city, ISNULL(region, 'N/A') AS region, country FROM Sales.Customers; custid city region country ----------- --------------- --------------- --------------- 7 Strasbourg N/A France 9 Marseille N/A France 32 Eugene OR USA 43 Walla Walla WA USA 45 San Francisco CA USA

Using COALESCE to Return Non-NULL Values 20761B Using COALESCE to Return Non-NULL Values 8: Using Built-In Functions COALESCE returns the first non-NULL value in a list: With only two arguments, COALESCE behaves like ISNULL If all arguments are NULL, COALESCE returns NULL COALESCE is standards-based COALESCE example: Note: Unlike NULLIF, COALESCE outputs a data type of the returned value. SELECT custid, country, region, city, country + ',' + COALESCE(region, ' ') + ', ' + city as location FROM Sales.Customers; custid country region city location ------ ------- ------ ----------- ---------------------- 17 Germany NULL Aachen Germany, , Aachen 65 USA NM Albuquerque USA,NM, Albuquerque 55 USA AK Anchorage USA,AK, Anchorage 83 Denmark NULL Århus Denmark, , Århus

Using NULLIF to Return NULL If Values Match 20761B Using NULLIF to Return NULL If Values Match 8: Using Built-In Functions NULLIF compares two expressions: Returns NULL if both arguments are equal Returns the first argument if the two arguments are not equal Note: The example query cannot be run in the sample database. Code to build the example is in the demonstration script file. Point out to the students that, by adding a test for NULL in the WHERE clause of the example query, rows with matching goal and actual values could be filtered out. The SQL Server 2016 Technical Documentation lists NULLIF as an expression rather than a function. emp_id goal actual 1 100 110 2 90 3 4 80 SELECT emp_id, NULLIF(actual,goal) AS actual_if_different FROM dbo.employee_goals; emp_id actual_if_different ----------- ------------------- 1 110 2 NULL 3 90 4 80

Demonstration: Using Functions to Work with NULL 20761B Demonstration: Using Functions to Work with NULL 8: Using Built-In Functions In this demonstration, you will see how to: Use functions to work with NULL Preparation Steps Complete the previous demonstration in this module. Demonstration Steps Use Functions to Work with NULL In Solution Explorer, open the 41 - Demonstration D.sql script file. Select the code under the comment Step 1, and then click Execute. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Select the code under the comment Step 4a, and then click Execute. Select the code under the comment Step 4b, and then click Execute. Select the code under the comment Step 4c, and then click Execute. Select the code under the comment Step 4d, and then click Execute. Select the code under the comment Step 5, and then click Execute. Close SQL Server Management Studio without saving any files.

Lab: Using Built-in Functions Exercise 3: Writing Queries That Test for Nullability Important: When comparing your results with the provided sample outputs, the column ordering and total number of affected rows should always match. However, remember that the order of the rows in the output of a query without an ORDER BY clause is not guaranteed. Therefore, the order of the rows in the sample outputs may be different to yours. In addition, the answer outputs include abbreviated results. Exercise 1: Writing Queries That Use Conversion Functions You have been asked to write the following reports for these departments: Sales. The product name and unit price for each product within an easy to read string. Marketing. The order id, order date, shipping date, and shipping region for each order after 4/1/2007. IT. Convert all Sales phone number information into integers. Exercise 2: Writing Queries That Use Logical Functions The sales department would like to have different reports regarding the segmentation of customers and specific order lines. You will add a new calculated column to show the target group for the segmentation. Exercise 3: Writing Queries That Test for Nullability The sales department would like to have additional segmentation of customers. Some columns that you should retrieve contain missing values, and you will have to change the NULL to some more meaningful information for the business users. Logon Information Virtual machine: 20761B-MIA-SQL User name: ADVENTUREWORKS\Student Password: Pa$$w0rd Estimated Time: 40 minutes

20761B Lab Scenario 8: Using Built-In Functions You are an Adventure Works business analyst, who will be writing reports using corporate databases stored in SQL Server. You have been provided with a set of business requirements for data and you will write T-SQL queries to retrieve the specified data from the databases. You will need to retrieve the data, convert it, and then check for missing values.

Module Review and Takeaways 20761B Module Review and Takeaways 8: Using Built-In Functions Best Practice Review Question(s) Question Which function should you use to convert from an int to a nchar(8)? Answer CAST() Which function will return a NULL, rather than an error message, if it cannot convert a string to a date? TRY_CONVERT(). What is the name for a function that returns a single value? A scalar function. Best Practice: When possible, use standards-based functions, such as CAST or COALESCE, rather than SQL Server-specific functions like NULLIF or CONVERT. Consider the impact of functions in a WHERE clause on query performance.