Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


February 28, 2008

New Data Types in SQL Server 2008

Spatial and date data types extend your reach
RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Executive Summary:

From new spatial data types such as GEOGRAPHY to new date data types such as DATETIMEOFFSET, SQL Server 2008 offers DBAs an easier way to solve problems and extend SQL Server to become an enterprise-class database platform. Three listings and inline code provide help with using these new data types.


SQL Server 2008 data types push into areas beyond simple relational data. These new data types give you more control over your database design, make it easier to migrate data from other systems, and can enhance application performance.

FILESTREAM
Without a doubt, the new data type I like best is the FILESTREAM data type. Designed to solve the problem of unstructured large object (LOB) storage, it combines the performance of file system streaming APIs with the transactional integrity of the relational database. With this data type, the unstructured data is stored in the NTFS file system, and the SQL Server engine manages the link between the FILESTREAM columns and the files in the file system—including backing up and restoring file system data. Unlike with the older IMAGE and VARBINARY(MAX) data types, the SQL Server buffer pool isn’t taken up with the retrieval of the LOB data.

Before working with the new FILESTREAM data type you need to enable it using the sp_filestream_configure stored procedure:

sp_filestream_configure @enable_level = 3

Next, because SQL Server uses a special type of filegroup for FILESTREAM access you must use the new CONTAINS FILESTREAM clause, which Listing 1 shows, when creating a database that will use the FILESTREAM objects.

After you create the database, you can create tables using the FILESTREAM data type. A table using a FILE-STREAM data type requires a column with a UNIQUEIDENTIFIER. Listing 2 shows how to declare and use the new FILESTREAM data type.

DATE and TIME
How long have we waited for these? Although DATETIME offers what you need, frequently you just want the date or the time. Plus, it was often a problem to convert data from other systems that used discreet date and time column values to and from the older DATETIME data type.

The new DATE data type is a native SQL Server data type that’s ANSI compliant. It uses the format YYYY-MM-DD and can contain values from 0001-01-01 to 9999-12-31.

Another new native SQL data type, the TIME data type, complements the DATE data type. TIME uses the format hh:mm:ss[.nnnnnnn] and can contain values from 00:00:00.0000000 to 23:59.59.9999999. To use it, enter the following:

CREATE TABLE dbo.MyDateTime
(MyDate DATE,
  
  MyTime TIME(3));
INSERT into dbo.MyDateTime
VALUES(‘12/25/07’, ‘22:18:48.123’)  

DATETIME2 and DATETIMEOFFSET
While DATE and TIME were designed to address simple needs for date and time storage, DATETIME2 and DATETIMEOFFSET are at the other end of the date/time continuum. Designed to address the need for more precise date/ time storage and accurate up to 100 nanoseconds, DATETIME2 uses the format YYYY-MM-DD hh:mm:ss[.nnnnnnn]. It can store values ranging from 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59.59.9999999.

DATETIMEOFFSET is like DATETIME2 except that it’s also time-zone aware. It uses the format YYYY-MM-DD hh:mm:ss[.nnnnnnn] and can store values ranging from 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59.59.9999999. Listing 3 shows how to use it.

HIERARCHYID
Designed to address the problem of tracking tree structures such as organization charts, the new HIERARCHYID is a variable-length system data type that stores data optimized for either depth or breadth. To help you work with it, Microsoft has included several methods to query and modify the hierarchical data including GetRoot(), GetLevel(), GetAncestor, and IsDescendant.

The code below shows how to declare a new HIERARCHYID data type. It uses the GetLevel method to create a breath-first ordering:

CREATE TABLE dbo.MyEmps
(MyEmpID HIERARCHYID,
    
  MyEmpLevel as MyEmpID.GetLevel(),
  
  MyEmpName VARCHAR(50));  

GEOGRAPHY and GEOMETRY
The integration of mapping capabilities into many applications makes the SQL Server 2008 spatial data types a welcome addition. GEOGRAPHY is a .NET-based spatial data type and uses a geodetic (round earth) model. It stores points, lines, polygons, and collections of latitude and longitude coordinates.

Another .NET-based data type, GEOMETRY uses a planar (flat earth) model, unlike GEOGRAPHY, which is primarily designed for navigation and mapping. GEOMETRY complies with Open Geospatial Consortium standards for the representation of geographic features.

To declare and assign values to GEOGRAPHY and GEOMETRY, use Listing 4.

Extending Your Reach
The new data types introduced in SQL Server 2008 solve some old problems. They also extend SQL Server 2008 beyond the realm of a traditional relational database server into an enterprise database platform.

End of Article



Reader Comments
pretty sloppy:
Listing 3: Using DATETIMEOFFSET
CREATE TABLE dbo.MyEmps
(
MyEmpID HIERARCHYID,
MyEmpLevel as MyEmpID.GetLevel(),
MyEmpName VARCHAR(50)
);

kevinoh47 January 15, 2009 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Learning Path To Learn More About Data Types
"Sharpen Your Skills: Joins, Groupings, and Data Types"


To Learn More About the DATETIME Data Type
"Help for the DATETIME data type and DST on SQL Server"


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Related Articles Can’t Wait for SQL Server 2008!

DATETIME Calculations, Part 1

T-SQL's Datetime Data Type

When Datetime Data Types Behave Strangely

Related Events SQL Server 2008 R2 Business Intelligence Enterprise Data Platform Conference & Expo

SQL Server Unleashed EMEA

Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement