Bài giảng Microsoft SQL server - Bài 2: Các kiểu dữ liệu và hàm trong SQL Server thông tin đến người học kiến thức về các kiểu dữ liệu; một số hàm chuyển đổi kiểu dữ liệu; cách sử dụng các hàm.
Lê Thị Tú Kiên - HQT SQL Server HỆ QUẢN TRỊ CƠ SỞ DỮ LIỆU SQL SERVER TS Lê Thị Tú Kiên kienltt@hnue.edu.vn Murach's SQL Server 2012, C8 http://fit.hnue.edu.vn/~kienltt/SQLSERVER/ Bài Các kiểu liệu hàm SQL Server Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide Nội dung Các kiểu liệu Một số hàm chuyển đổi kiểu liệu Cách sử dụng hàm Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide SQL Server data type categories (Các kiểu liệu SQL Server chia làm nhóm chính) String (Xâu kí tự) Numeric (Số) Temporal (date/time) Other (Một số kiểu khác) Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide ANSI-standard data types and SQL Server equivalents (Bảng tương ứng kiểu liệu SQL Server với kiểu liệu chuẩn ANSI) Synonym for ANSI-standard data type binary varying char varying character dec double precision float integer Murach's SQL Server 2012, C8 SQL Server data type used varbinary varchar character varying char decimal float real or float int Lê Thị Tú Kiên - HQT SQL Server Slide ANSI-standard data types and SQL Server equivalents (continued) Synonym for ANSI-standard data type national char national char varying national text rowversion Murach's SQL Server 2012, C8 SQL Server data type used nchar national character nvarchar national character varying ntext timestamp Lê Thị Tú Kiên - HQT SQL Server Slide The integer data types (Các kiểu DL số nguyên) Type bigint int smallint tinyint bit Murach's SQL Server 2012, C8 Bytes 1 Lê Thị Tú Kiên - HQT SQL Server Slide The decimal data types (Kiểu liệu số thập phân) Type decimal[(p[,s])] numeric[(p[,s])] money smallmoney Murach's SQL Server 2012, C8 Bytes 5-17 5-17 Lê Thị Tú Kiên - HQT SQL Server Slide The real data types (Kiểu số thực) Type float[(n)] real Murach's SQL Server 2012, C8 Bytes or Lê Thị Tú Kiên - HQT SQL Server Slide String data types for storing standard characters (Kiểu xâu kí tự lưu trữ kí tự chuẩn) Type char[(n)] varchar[(n)] Bytes n String data types for storing Unicode characters (Kiểu xâu kí tự lưu trữ kí tự Unicode) Type nchar(n) nvarchar(n) Murach's SQL Server 2012, C8 Bytes 2×n Lê Thị Tú Kiên - HQT SQL Server Slide 10 A SELECT statement that substitutes a different data type SELECT VendorName, COALESCE(CAST(InvoiceTotal AS varchar), 'No invoices') AS InvoiceTotal FROM Vendors LEFT JOIN Invoices ON Vendors.VendorID = Invoices.VendorID ORDER BY VendorName; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 64 The syntax of the GROUPING function GROUPING(column_name) A summary query with a GROUPING function SELECT CASE WHEN GROUPING(VendorState) = THEN 'All' ELSE VendorState END AS VendorState, CASE WHEN GROUPING(VendorCity) = THEN 'All' ELSE VendorCity END AS VendorCity, COUNT(*) AS QtyVendors FROM Vendors WHERE VendorState IN ('IA', 'NJ') GROUP BY VendorState, VendorCity WITH ROLLUP ORDER BY VendorState DESC, VendorCity DESC; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 65 The result set Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 66 The syntax for the four ranking functions ROW_NUMBER() OVER ([partition_by_clause] order_by_clause) RANK() OVER ([partition_by_clause] order_by_clause) DENSE_RANK() OVER ([partition_by_clause] order_by_clause) NTILE(integer_expression) OVER ([partition_by_clause] order_by_clause) Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 67 A query with a ROW_NUMBER function SELECT ROW_NUMBER() OVER(ORDER BY VendorName) AS RowNumber, VendorName FROM Vendors; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 68 A query that uses the PARTITION BY clause SELECT ROW_NUMBER() OVER(PARTITION BY VendorState ORDER BY VendorName) As RowNumber, VendorName, VendorState FROM Vendors; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 69 A query with RANK and DENSE_RANK functions SELECT RANK() OVER (ORDER BY InvoiceTotal) As Rank, DENSE_RANK() OVER (ORDER BY InvoiceTotal) As DenseRank, InvoiceTotal, InvoiceNumber FROM Invoices; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 70 A query that uses the NTILE function SELECT TermsDescription, NTILE(2) OVER (ORDER BY TermsID) AS Tile2, NTILE(3) OVER (ORDER BY TermsID) AS Tile3, NTILE(4) OVER (ORDER BY TermsID) AS Tile4 FROM Terms; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 71 The syntax of the analytic functions {FIRST_VALUE|LAST_VALUE}(scalar_expression) OVER ([partition_by_clause] order_by_clause [rows_range_clause]) {LEAD|LAG}(scalar_expression [, offset [, default]]) OVER ([partition_by_clause] order_by_clause) {PERCENT_RANK()|CUME_DIST} OVER ([partition_by_clause] order_by_clause) {PERCENTILE_CONT|PERCENTILE_DISC}(numeric_literal) WITHIN GROUP (ORDER BY expression [ASC|DESC]) OVER (partition_by_clause) Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 72 The columns in the SalesReps table Column name RepID RepFirstName RepLastName Data type int varchar(50) varchar(50) The columns in the SalesTotals table Column name RepID SalesYear SalesTotal Murach's SQL Server 2012, C8 Data type int char(4) money Lê Thị Tú Kiên - HQT SQL Server Slide 73 A query that uses the FIRST_VALUE and LAST_VALUE functions SELECT SalesYear, RepFirstName + ' ' + RepLastName AS RepName, SalesTotal, FIRST_VALUE(RepFirstName + ' ' + RepLastName) OVER (PARTITION BY SalesYear ORDER BY SalesTotal DESC) AS HighestSales, LAST_VALUE(RepFirstName + ' ' + RepLastName) OVER (PARTITION BY SalesYear ORDER BY SalesTotal DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LowestSales FROM SalesTotals JOIN SalesReps ON SalesTotals.RepID = SalesReps.RepID; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 74 The result set Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 75 A query that uses the LAG function SELECT RepID, SalesYear, SalesTotal AS CurrentSales, LAG(SalesTotal, 1, 0) OVER (PARTITION BY RepID ORDER BY SalesYear) AS LastSales, SalesTotal - LAG(SalesTotal, 1, 0) OVER (PARTITION BY REPID ORDER BY SalesYear) AS Change FROM SalesTotals; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 76 A query that uses four more functions SELECT SalesYear, RepID, SalesTotal, PERCENT_RANK() OVER (PARTITION BY SalesYear ORDER BY SalesTotal) AS PctRank, CUME_DIST() OVER (PARTITION BY SalesYear ORDER BY SalesTotal) AS CumeDist, PERCENTILE_CONT(.5) WITHIN GROUP (ORDER BY SalesTotal) OVER (PARTITION BY SalesYear) AS PercentileCont, PERCENTILE_DISC(.5) WITHIN GROUP (ORDER BY SalesTotal) OVER (PARTITION BY SalesYear) AS PercentileDisc FROM SalesTotals; Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 77 Terms Logical functions Ranking functions Analytic functions Murach's SQL Server 2012, C8 Lê Thị Tú Kiên - HQT SQL Server Slide 78 ... EOMONTH( ''20 1 2- 0 2- 01'') EOMONTH( ''20 1 2- 0 2- 01'' ,2) DATEFROMPARTS (20 12, 4,3) 20 1 2- 0 2- 29 20 1 2- 0 4-3 0 20 1 2- 0 4-0 3 ISDATE( ''20 1 2- 0 9-3 0'') ISDATE( ''20 1 2- 0 9-3 1'') ISDATE( ''23 :59:59'') ISDATE( ''23 :99:99'') 1 Murach''s SQL. .. DATEDIFF(quarter, ''20 1 1- 1 2- 01'', ''20 1 2- 0 9-3 0'') DATEDIFF(week, ''20 1 1- 1 2- 01'', ''20 1 2- 0 9-3 0'') DATEDIFF(day, ''20 1 2- 0 9-3 0'', ''20 1 1- 1 2- 01'') 304 28 9 1 729 5 44 -3 04 Murach''s SQL Server 20 12, C8 Lê Thị Tú Kiên - HQT SQL. .. 20 1 2- 0 9-3 0 20 1 2- 0 9-3 0 20 1 2- 0 9-3 0 20 1 2- 1 2- 30 20 1 2- 1 0-0 7 20 1 2- 0 8-3 0 20 1 3-0 9-3 0 Murach''s SQL Server 20 12, C8 Lê Thị Tú Kiên - HQT SQL Server 11:35:00.000 11:35:00.000 11:35:00.000 12: 35:00.000 11:36:00.000