Click here to Skip to main content
15,881,455 members
Articles / Database Development / SQL Server

Uncommon SQL Server Data Types

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
29 Dec 2016MIT5 min read 5.7K   1  
Uncommon SQL server data types

In this article, we’re going to go over uncommon SQL server data types. These are the ones you won’t use every day, but you’ll want to know if you take the 70-461 exam.

Even if you don’t plan on taking the 70-461, learning these data types is fun, especially the spatial datatype (I had no idea you could “draw” shapes using SQL Server Management Studio).

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database. You can get started using these free tools using my Guide Getting Started Using SQL Server.

Uncommon SQL Server Data Types

There are over thirty different data types you can choose from when defining columns. Many of these are set up for very specific jobs such as storing images, and others more suitable to general use.

In our article, Commonly used SQL Server Datatypes, we cover the types you most use in your database. In this article, we cover three more. In addition to those commonly used, these are ones you’ll find used within problems on the 70-461 exam.

They are:

  • XML
  • Uniqueidentifier
  • Spatial Data

XML –XML (Extensible Markup Language) Data

Before we begin to talk about how SQL Server can use XML data, let’s first understand what XML data really is.

XML stands for Extensible Markup Language. By itself, XML does nothing. Its main purpose is to store and transport data.

Here is an example of XML data:

XML
<class>
   <name>Introduction to SQL</name>
   <instructor>C. J. Date</instructor>
   <number>EECS 475</number>
   <description>Discover and learn relation databases using SQL</description>
 </class>

XML data is described by elements and attributes. An example of an element is <class>.

I won’t go much further into XML here. We’ll make the assumption you generally understand what XML is, the difference between attributes and elements, and how a schema defines the structure of an XML document.

If you are new to XML, may I recommend you read A Really, Really, Really Good Introduction to XML.

OK, so the XML data type allows you to store XML data associated with a schema. Knowing the schema then allows you to parse the XML and extract specific elements such as the Instructor name.

This is how you can define a variable using the XML type:

SQL
DECLARE @myVariable xml (xmlSchema);

For example:

SQL
DECLARE @x xml (Production.ProductDescriptionSchemaCollection);

When XML data has a schema associated with it, it is said to be typed. When you have a schema which can be used to check the structure of XML, it may make sense to store the XML data as an XML type.

XML is very readable and you may have noticed it is just text. Given this, why not just store XML in a VARCHAR field and call it quits?

You could and there are reasons to do so, but before we make a decision to settle with VARCHAR as you self these questions to determine when should you choose type versus untyped XML.

Use untyped XML if:

  • Your XML doesn’t have a schema.
  • You have a schema, but, you don’t need SQL Server to validate the data as the data is validated by a client program, or the data is just temporarily stored in SQL server to be used and processed elsewhere.

Use typed XML if:

  • Your XML has a schema and you want SQL Server to validate the data against it.
  • You want to take advantage of storage and query optimizations based on type information.
  • You want to take better advantage of type information during compilation of your queries.

If you’re looking for an example of an XML type field in AdventureWorks 2012, check out the Person table.

Uncommon SQL Server Data Types - XML

Here, you see the Demographics column is defined as XML type and bound to the Person.IndividualSurveySchemaCollection.

Here is an example of the Demographics data shown in the first 10 rows:

Uncommon SQL Server Data Types - XML Data

In another article, we go into further detail on how you can query for specific values within the elements.

Unique Identifier – A Globally Unique ID (GUID)

The uniqueidentifier type is used when you wish to store a GUID (Globally Unique ID). The main purpose to create GUID is create an ID that is unique across many computers within a network.

Unique Identifiers take the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx where x represents a hexadecimal value (e.g. 0-F).

An example of an GUID is F22620D0-600E-4F0D-86E3-71250D1CE01E.

You can use the NEWID() function to generate GUIDs.

Here is an example:

SQL
DECLARE @myGUID uniqueidentifier = NEWID();
SELECT @myGUID

Which when I ran it returned 0AFEBE69-7B1E-43F9-909E-35E7E32535B2. When you run it, it will create a different GUID as you’re running it on a different computer at another time.

Unique Identifiers are important to SQL as they’re used when replicating data.

Spatial Data – Geometric and Geographic Data

There are several spatial data types supported in SQL Server. Rather than get into specifics on each one, we’ll provide an overview of what spatial data types are, their purpose, and a very brief example of their use.

As mapping and other means to visualize data become more important, having a means to manipulate spatial data such as geometric or geographic data becomes more and more relevant.

In SQL Server, spatial data covers both geometric data such as points, curves, and polygons, as well as geographic data.

If you’re looking for a really good overview on Spatial data, I would recommend looking over the MDSN article Spatial Data Types Overview or Redgate’s Introduction to SQL Server Spatial Data.

Here is an example of a query which creates a square and triangle as geometry types, then though a UNION, selects both objects and returns them as a single result:

SQL
DECLARE @sqr geometry, @tri geometry;
SET @sqr = geometry::STGeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326);
SET @Tri = geometry::STGeomFromText('POLYGON((5 5,10 15,15 5, 5 5))', 4326);
SELECT @sqr
UNION ALL
SELECT @tri

Source: SQL From the Trenches

When you look at the result, the data grid is pretty unsurprising; however, you’ll see there is a new Spatial Result tab. Here, you’ll see a visualization of the query results!

Uncommon SQL Server Data Types - Spatial Data

The post Uncommon SQL Server Data Types appeared first on Essential SQL.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --