Query optimization how to search millions of record in sql table faster -
New date datatypes in sql server 2008 tech republic
1. New Date Datatypes in SQL Server 2008 | TechRepublic
ZDNet Asia SmartPlanet TechRepublic Log In Join TechRepublic FAQ Go Pro!
Blogs Downloads Newsletters Galleries Q&A Discussions News
Research Library
IT Management Development IT Support Data Center Networks Security
Home / Blogs / The Enterprise Cloud Follow this blog:
The Enterprise Cloud
New Date Datatypes in SQL
Server 2008
By Tim Chapman
September 8, 2008, 10:59 AM PDT
Takeaway: SQL Server 2008 provides new data-types for storing wider ranges of dates with
more precision. Heres can you can use these new data types to your advantage in SQL Server
2008.
As a database architect, one of the features I am most excited about in SQL Server 2008 is the
new date related data types. These data types allow for the storage of time-only data, date-only
data, a larger range of date values, more time precision, and the ability to store time-zone specific
dates. Today I’ll take a look at these great new data types and how they can be used to expand
Btrieve 6.15 Forever
your date storage possibilities.
Still using Btrieve? So are we! Get the Ultimate
DATE
Btrieve Patch
pervasivedb.com/btrieve
The new DATE data-type defines a date field without any associated time. In previous versions of Improve Productivity
SQL Server, storing time-only data was accomplished in one of two ways. First, you could define Apps For Business- 30 Day Free Trial. No
either a DATETIME or SMALLDATETIME data type and assign the value as a VARCHAR value. Credit Card Required!
Then you would need to handle the time related data in your queries. The other way is to store the www.google.com/apps
date value in a VARCHAR field in the database. Either way, handling date-only data was at best OTM (G-Log) Solutions
cumbersome. With the new DATE data-type, it intrinsically stores only date values. In the following At MavenWire, We Offer Solutions to Meet
example I define a DATE data-type and a DATETIME data type and assign today’s date to the Your Business Needs. Visit Us!
variables. As you can see from SELECTing the values from the variables, the DATE variable holds www.MavenWire.com
only the date portion whereas the DATETIME field also stores the associated time from the
GETDATE() function.
DECLARE @TodaysDate DATE = GETDATE()
DECLARE @TodaysDateTime DATETIME = GETDATE()
Keep Up with TechRepublic
SELECT @TodaysDate, @TodaysDateTime
Storing DATE specific data will also make queries easier to write. Instead of using one of the
following statements to pull data for a DATETIME column;
Five Apps
SELECT * FROM SalesHistory
Google in the Enterprise
WHERE SaleDate = ‘4/11/2008' AND SaleDate ‘4/12/2008'
you can use an equality operator (=) to search only for the specific date, assuming the Subscribe Today
SaleDateOnly field is defined as a DATE data-type.
SELECT * FROM SalesHistory Follow us however you choose!
WHERE SaleDateOnly = ‘4/11/2008'
http://www.techrepublic.com/blog/datacenter/new-date-datatypes-in-sql-server-2008/443[08/29/2012 3:44:51 PM]
2. New Date Datatypes in SQL Server 2008 | TechRepublic
DATETIME2
This data type is very simiilar to the datetime data type present in previous versions of SQL
Server. The difference is the time precision and the range of dates that DATETIME2 can store.
The new DATETIME2 data type can hold a range of dates from 0001-01-01 through 9999-12-31
while DATETIME holds the date range from 1753-01-01 to 9999-12-31. Another benefit of this Media Gallery
data type is the precision in which it records time. DATETIME2 is accurate to within 100
nanoseconds whereas DATETIME is accurate to within 3/1000 of a second. The following script
compares the values that the DATETIME and the DATETIME2 data-types can hold.
DECLARE @DateTime2 DATETIME2 = GETDATE()
DECLARE @DateTime DATETIME = GETDATE() PHOTO GALLERY (1 of 15)
SELECT @DateTime, @DateTime2
Curiosity's autonomous
'seven minutes of...
Notice the additional precision stored in the DATETIME2 variable. This additional data can be of
More Galleries »
major significance for those applications where time related information is mission-critical.
TIME
The TIME data-type stores only the time portion of a date. This is significant in that the time data
can be stored seperately and not associated with a specific date. The TIME data-type, like the
DATETIME2 data-type is accurate up to 100 nanoseconds. The following script compares the time
specific information returned by the TIME data type and that which is returned from a DATETIME VIDEO (1 of 13)
data type. Cracking Open: HTC Titan II
DECLARE @Time TIME = GETDATE() More Videos »
DECLARE @DateTime DATETIME = GETDATE()
SELECT @Time, @DateTime
Hot Questions View All
DATETIMEOFFSET
3 SSL redirection
The new DATETIMEOFFSET data type combines the range and precision of the DATETIME2
data-type along with time-zone awareness based on a 24 hour clock. The time-zone awareness is
accomplished by adding or subtracting an hours + minutes value to the date data. In the following
example, I define a temporary table that holds two fields. The first field holds the date that is local 2 can anyone suggest if any such
to my area. The second field, BeijingTime, is an offset date that will hold the time in Beijing, China; software exist with similar
which happens to be 12 hours ahead of the time in Louisville, KY. functionality?
CREATE TABLE #OffSets
3 Switching from a Job to a career in
( the IT field: Need an IT pro's
LouisvilleTime DATETIME2 DEFAULT(GETDATE()),
advice
BeijingTime AS CONVERT(VARCHAR(50), LouisvilleTime, 121) + '+12:00'
2 windows 7 won't shutdown and
) keeps switching on
INSERT INTO #OffSets(LouisvilleTime)
SELECT GETDATE() Ask a Question
SELECT * FROM #OffSets
As you can see from the results from the table, the BeijingTime field holds the date from the
LouisvilleTime field along with a 12 hour offset to indicate Beijing time. This data-type greatly Hot Discussions View All
enhances the ability to create time-zone aware applications.
221 Should developers be sued for
TIME to try the new data-types security holes?
The new date related data-types included in SQL Server 2008 greatly expand the database
developers ability to not only store a wider range of dates and precision for those dates, but also 79 The sitting duck that is open
the ability to zone time-zone specific data which can lead to more robust and accurate reporting of source
time-related data around the world.
27 Five fast Windows desktop search
utilities
Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free
newsletters. 30 Is the death knell sounding for
traditional antivirus?
http://www.techrepublic.com/blog/datacenter/new-date-datatypes-in-sql-server-2008/443[08/29/2012 3:44:51 PM]
3. New Date Datatypes in SQL Server 2008 | TechRepublic
Start a Discussion
About Tim Chapman
Full Bio Contact Blog Archive
August 2012 December 2011
July 2012 November 2011
June 2012 October 2011
Options for passing a driver Restarting Active Directory
May 2012 September 2011
into the Windows Server as a service in Windows
2008 install program Server 2008 April 2012 August 2011
March 2012 July 2011
People who read this... February 2012 June 2011
January 2012
No change in SQL Server 2008 pricing, says Microsoft
SQL server installation
SQL Server 2008 - How to uninstall or Delete unused Multiple Instances
10 tips for upgrading to SQL Server 2008 R2
5 Join the conversation! Add Your Opinion
Comments Follow via:
Staff Picks Top Rated Most Recent My Contacts See All Comments
Calendar issues 0
rwilson@... 24th Feb 2009 Votes
Odd that it can go so far forward but not back far enough for some historical,
geological, or anthropoligical data Actually, I was kind of wondering about the ability
to go past 1753 with any... Read Whole Comment +
View in thread
Examples 0
chapman.tim@... 10th Sep 2008 Votes
Sure, here are some examples:
DATE:
2007-05-08
TIME:
12:35:29. 1234567
DATETIME2:
2007-05-08 12:35:29. 1234567
DATETIMEOFFSET:
2007-05-08 12:35:29.1234567 +12:15
View in thread
http://www.techrepublic.com/blog/datacenter/new-date-datatypes-in-sql-server-2008/443[08/29/2012 3:44:51 PM]
4. New Date Datatypes in SQL Server 2008 | TechRepublic
RE: New Date Datatypes in SQL Server 2008 0
dwarsham@... 10th Sep 2008 Votes
Its about time we get a DATE type! I always tested date ranges like this: SELECT *
FROM SalesHistory WHERE SaleDate Between @SaleDate AND @SaleDate + '
11:59 pm' Of course @SaleDate was formatted... Read Whole Comment +
View in thread
See all comments
Join the TechRepublic Community and join the conversation! Signing-up is
free and quick, Do it now, we want to hear your opinion.
Join Login
http://www.techrepublic.com/blog/datacenter/new-date-datatypes-in-sql-server-2008/443[08/29/2012 3:44:51 PM]