SQL Server Advanced Data Types: JSON, XML, and Beyond
()
About this ebook
Widely used types such as JSON and XML are well-supported by the database engine. The same is true of hierarchical data and even temporal data. Knowledge of these advanced types is crucial to unleashing the full power that's available from your organization's SQL Server database investment.
SQL Server Advanced Data Types explores each of the complex data types supplied within SQL Server. Common usage scenarios for each complex data type are discussed, followed by a detailed discussion on how to work with each data type. Each chapter demystifies the complex data and you learn how to use the data types most efficiently.
The book offers a practical guide to working with complex data, using real-world examples to demonstrate how each data type can be leveraged. Performance considerations are also discussed, including the implementation of special indexes such as XML indexes and spatial indexes.
What You'll Learn
- Understand the implementation of basic data types and why using the correct type is so important
- Work with XML data through the XML data type
- Construct XML data from relational result sets
- Store and manipulate JSON data using the JSON data type
- Model and analyze spatial data for geographic information systems
- Define hierarchies and query them efficiently through the HierarchyID type
SQL Server developers and application developers who need to store and access complex data structures
Read more from Peter A. Carter
Securing SQL Server: DBAs Defending the Database Rating: 0 out of 5 stars0 ratingsPro SQL Server 2019 Administration: A Guide for the Modern DBA Rating: 0 out of 5 stars0 ratingsSQL Server 2019 AlwaysOn: Supporting 24x7 Applications with Continuous Uptime Rating: 0 out of 5 stars0 ratings
Related to SQL Server Advanced Data Types
Related ebooks
Pro T-SQL 2019: Toward Speed, Scalability, and Standardization for SQL Server Developers Rating: 0 out of 5 stars0 ratingsDynamic SQL: Applications, Performance, and Security in Microsoft SQL Server Rating: 0 out of 5 stars0 ratingsSQL Server Big Data Clusters: Data Virtualization, Data Lake, and AI Platform Rating: 0 out of 5 stars0 ratingsDomain-Driven Laravel: Learn to Implement Domain-Driven Design Using Laravel Rating: 0 out of 5 stars0 ratingsJoe Celko's Trees and Hierarchies in SQL for Smarties Rating: 0 out of 5 stars0 ratingsBeginning T-SQL: A Step-by-Step Approach Rating: 0 out of 5 stars0 ratingsMicrosoft Azure Cosmos DB Revealed: A Multi-Model Database Designed for the Cloud Rating: 0 out of 5 stars0 ratingsCosmos DB for MongoDB Developers: Migrating to Azure Cosmos DB and Using the MongoDB API Rating: 0 out of 5 stars0 ratingsSplunk Certified Study Guide: Prepare for the User, Power User, and Enterprise Admin Certifications Rating: 0 out of 5 stars0 ratingsCreating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5SQL Interview Questions: A complete question bank to crack your ANN SQL interview with real-time examples Rating: 0 out of 5 stars0 ratingsExploring the .NET Core 3.0 Runtime: Through Code Generation and Metadata Inspection Rating: 0 out of 5 stars0 ratingsRPG TnT: 101 Dynamite Tips 'n Techniques with RPG IV Rating: 5 out of 5 stars5/5Beginning SQL Server Reporting Services Rating: 0 out of 5 stars0 ratingsBeginning Apache Spark Using Azure Databricks: Unleashing Large Cluster Analytics in the Cloud Rating: 0 out of 5 stars0 ratingsSQL 101 Crash Course: Comprehensive Guide to SQL Fundamentals and Practical Applications Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 4 out of 5 stars4/5Learning SQL: Master SQL Fundamentals Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Entity Framework Tutorial - Second Edition Rating: 0 out of 5 stars0 ratingsElements of Android Room Rating: 0 out of 5 stars0 ratingsBeginning jOOQ: Learn to Write Efficient and Effective Java-Based SQL Database Operations Rating: 0 out of 5 stars0 ratingsOracle SQL Revealed: Executing Business Logic in the Database Engine Rating: 0 out of 5 stars0 ratingsWhat's New in SQL Server 2012 Rating: 0 out of 5 stars0 ratingsLearn SQL: Database Management Basics Rating: 0 out of 5 stars0 ratingsPolyBase Revealed: Data Virtualization with SQL Server, Hadoop, Apache Spark, and Beyond Rating: 0 out of 5 stars0 ratingsAdvanced Analytics with Transact-SQL: Exploring Hidden Patterns and Rules in Your Data Rating: 0 out of 5 stars0 ratingsCreating ASP.NET Core Web Applications: Proven Approaches to Application Design and Development Rating: 0 out of 5 stars0 ratingsSecuring SQL Server: Protecting Your Database from Attackers Rating: 0 out of 5 stars0 ratings
Databases For You
Blockchain Basics: A Non-Technical Introduction in 25 Steps Rating: 5 out of 5 stars5/5Practical Data Analysis Rating: 4 out of 5 stars4/5Access 2019 For Dummies Rating: 0 out of 5 stars0 ratings100+ SQL Queries T-SQL for Microsoft SQL Server Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Access 2016 For Dummies Rating: 0 out of 5 stars0 ratingsPython Projects for Everyone Rating: 0 out of 5 stars0 ratingsLearn SQL Server Administration in a Month of Lunches Rating: 3 out of 5 stars3/5SQL Clearly Explained Rating: 5 out of 5 stars5/5Artificial Intelligence for Fashion: How AI is Revolutionizing the Fashion Industry Rating: 0 out of 5 stars0 ratingsQuery Store for SQL Server 2019: Identify and Fix Poorly Performing Queries Rating: 0 out of 5 stars0 ratingsCOBOL Basic Training Using VSAM, IMS and DB2 Rating: 5 out of 5 stars5/5Developing High Quality Data Models Rating: 0 out of 5 stars0 ratingsOracle DBA Mentor: Succeeding as an Oracle Database Administrator Rating: 0 out of 5 stars0 ratingsData Governance: How to Design, Deploy and Sustain an Effective Data Governance Program Rating: 4 out of 5 stars4/5Text Analytics with Python: A Practitioner's Guide to Natural Language Processing Rating: 0 out of 5 stars0 ratingsSQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5Beginning Microsoft Power BI: A Practical Guide to Self-Service Data Analytics Rating: 0 out of 5 stars0 ratingsBuilding a Scalable Data Warehouse with Data Vault 2.0 Rating: 4 out of 5 stars4/5Business Intelligence Strategy and Big Data Analytics: A General Management Perspective Rating: 5 out of 5 stars5/5Access 2010 All-in-One For Dummies Rating: 4 out of 5 stars4/5Data Mining: Concepts and Techniques Rating: 4 out of 5 stars4/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsServerless Architectures on AWS, Second Edition Rating: 5 out of 5 stars5/5Data Science Strategy For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for SQL Server Advanced Data Types
0 ratings0 reviews
Book preview
SQL Server Advanced Data Types - Peter A. Carter
© Peter A. Carter 2018
Peter A. CarterSQL Server Advanced Data Typeshttps://doi.org/10.1007/978-1-4842-3901-8_1
1. SQL Server Data Types
Peter A. Carter¹
(1)
London, UK
SQL Server 2017 provides a wide range of basic data types that can store data, such as character strings, numeric data, binary data, and dates. In this chapter, I will review each of the basic data types available in SQL Server, looking at the differences between similar data types. I will also discuss the importance of using the correct data type for each use case.
Tip
The following sections discuss the acceptable ranges for each data type. It is worth noting, however, that all data types can also be set to NULL, indicating that the value is unknown.
Numeric Data Types
Some numeric data types in SQL Server are exact, while others are approximate. If a data type is exact, it stores a number to a fixed precision. If a data type is approximate, in many cases, it will store a very close approximation of a number, instead of the number itself.
The numeric data types supported by SQL Server are listed in Table 1-1.
Table 1-1
Numeric Data Types
Tip
The caret (^) operator means to the power. For example, 2^15-1 means 2 to the power of 15 minus 1, which equates to 32,767.
To see how a number behaves, using each of the numeric formats, we will use the number 2.5888712345678923456789 and convert it to each of the numeric data types, to examine its behavior. In SQL Server, there are two functions that we can use to change the data type of a value: CAST and CONVERT.
The CAST function uses the syntax in Listing 1-1.
CAST( expression AS datatype[(length)] )
Listing 1-1
CAST Syntax
The expression can be either a value that you wish to convert, a column in a table, or any other valid expression. The datatype should be the target data type for the expression. length is optional and based on the target data type. For example, if you were using CAST to change the data type of an INT to a DECIMAL, length would consist of the required precision and scale. If you were casting a value to a string, the length would be the maximum length of the string.
The CONVERT function uses the syntax in Listing 1-2.
CONVERT( datatype[(length)], expression [, style] )
Listing 1-2
CONVERT Syntax
The style option of the CONVERT function allows the developer to supply additional information about how to translate the expression. The style options that are available for numeric data types can be found in Table 1-2.
Table 1-2
Numeric Data Type Style Options
The script in Listing 1-3 shows how to use the CAST function to translate the number 2.5888712345678923456789 to each numeric data type.
Note
BIT is excluded, as the conversion does not make sense. If it were included, however, it would CAST to 1.
SELECT
CAST(2.5888712345678923456789 AS TINYINT) AS 'TINYINT'
, CAST(2.5888712345678923456789 AS SMALLINT) AS 'SMALLINT'
, CAST(2.5888712345678923456789 AS INT) AS 'INT'
, CAST(2.5888712345678923456789 AS BIGINT) AS 'BIGINT'
, CAST(2.5888712345678923456789 AS DECIMAL(23,22)) AS 'DECIMAL'
, CAST(2.5888712345678923456789 AS DECIMAL(18,17)) AS 'DECIMAL ROUNDED'
, CAST(2.5888712345678923456789 AS NUMERIC(23,22)) AS 'NUMERIC'
, CAST(2.5888712345678923456789 AS FLOAT(24)) AS 'SINGLE FLOAT'
, CAST(2.5888712345678923456789 AS FLOAT(53)) AS 'DOUBLE FLOAT'
, CAST(2.5888712345678923456789 AS REAL) AS 'REAL'
, CAST(2.5888712345678923456789 AS SMALLMONEY) AS 'SMALLMONEY'
, CAST(2.5888712345678923456789 AS MONEY) AS 'MONEY'
Listing 1-3
Casting a Number to Each Data Type
The results of the query in Listing 1-3 can be found in Figure 1-1.
../images/447873_1_En_1_Chapter/447873_1_En_1_Fig1_HTML.pngFigure 1-1
Results of casting to numeric data types
Character Strings
SQL Server can store both Unicode and non-Unicode strings. Strings can also be stored either as a fixed length or a variable length. The character data types supported by SQL Server are listed in Table 1-3.
Table 1-3
Character Data Types
The script in Listing 1-4 uses the DATALENGTH system function to demonstrate the difference in storage size for a 15-character string, cast as each character data type.
SELECT
DATALENGTH(CAST('My String Value' AS NCHAR(20))) AS 'NCHAR'
, DATALENGTH(CAST('My String Value' AS NVARCHAR(20))) AS 'NVARCHAR'
, DATALENGTH(CAST('My String Value' AS CHAR(20))) AS 'CHAR'
, DATALENGTH(CAST('My String Value' AS VARCHAR(20))) AS 'VARCHAR'
Listing 1-4
Examining the Storage Size of Strings
The results of Listing 1-4 can be found in Figure 1-2.
../images/447873_1_En_1_Chapter/447873_1_En_1_Fig2_HTML.jpgFigure 1-2
Results of comparing string storage sizes
Binary Data Types
SQL Server can store binary data, such as a word document or a photo, using native binary data types. Binary data types are also used to store data that has been encrypted using a key or certificate. The binary data types available in SQL Server are detailed in Table 1-4.
Table 1-4
Binary Data Types
Tip
For detailed information about encrypting data, please refer to Securing SQL Server (Apress, 2016), which can be found at www.apress.com/gp/book/9781484222645 .
The style options available for BINARY data when using the CONVERT function are detailed in Table 1-5.
Table 1-5
Style Options for BINARY Data
The script in Listing 1-5 demonstrates how to read a password that has been encrypted and stored in a VARBINARY column and convert it back to a character string. The script first creates the required objects and encrypts the password.
--Create a certificate that will encrypt the symmetric key
CREATE CERTIFICATE PasswordCert
ENCRYPTION BY PASSWORD = 'MySecurePa$$word'
WITH SUBJECT = 'Cert for securing passwords table' ;
--Create a symmetric key that will encrypt the password
CREATE SYMMETRIC KEY PasswordKey
WITH ALGORITHM = AES_128
ENCRYPTION BY CERTIFICATE PasswordCert ;
--Create a table to store the password
CREATE TABLE dbo.Passwords
(
Password VARBINARY(256)
) ;
--Open the symmetric key, so that it can be used
OPEN SYMMETRIC KEY PasswordKey
DECRYPTION BY CERTIFICATE PasswordCert
WITH PASSWORD = 'MySecurePa$$word' ;
--Encrypt a password and insert it into the table
INSERT INTO dbo.Passwords
SELECT ENCRYPTBYKEY(KEY_GUID('PasswordKey'), 'Pa$$w0rd') ;
--Decrypt and read the password
--The first column in the result set shows the password as the decrypted value but still binary format
--The second column in the result set shows the password decrypted and converted back to a character string
SELECT
DECRYPTBYKEY(Password) AS 'Decrypted Password In Binary'
, CAST(DECRYPTBYKEY(Password) AS CHAR(8)) AS 'Decrypted Password As Character String'
FROM dbo.Passwords
--Close the symmetric key
CLOSE SYMMETRIC KEY PasswordKey ;
Listing 1-5
Decrypting an Encrypted Password and Converting It Back to a Character String
The results of the select statement against the table created in Listing 1-5 can be found in Figure 1-3.
../images/447873_1_En_1_Chapter/447873_1_En_1_Fig3_HTML.jpgFigure 1-3
Results of converting binary data to a character string
Dates and Times
SQL Server can store accurate date and time information, including UTC offsets. The details of each supported date and time data type can be found in Table 1-6.
Table 1-6
Date and Time Data Types
Table 1-7 details allowable style options