I am finding SQL Server 2008 very exciting. Every day it's a new learning for me and my eyes glitter seeing the sweet surprises that SQL Server 2008 is bringing in for efficient data management. I like SQL Server because I realize that data is like the life blood for any business, any forecasting any strategic decisions to be taken depends on the facts and figures. When so many things depend on data then efficient presentation of data with precision is most important. Precision in Date and Time is important thing that we have to maintain. When the business is growing globally, it needs to maintain the Time Zones, precision of time in nanoseconds etc. So to cater to such requirements SQL Server 2008 - (Katmai) has introduced new date/time data types.
Please look at the table (7x7 Matrix) to take a quick overview of the Data Types :
1) TIME : This data type is useful to define the time of the day, this data type is not Time Zone aware and it is based on a 24 hour clock. This data type is not aware of Day Light Saving. This data type is capable of handling high precision time in a small storage space.
The storage space taken by the TIME data type is as follows:
2) DATE : This data type is useful to store the dates without the time part, we can store dates starting from 00001-01-01 through 9999-12-31 i.e. January 1, 1 A.D. through December 31, 9999 A.D. It supports the Gregorian Calendar and uses 3 bytes to store the date.
Create Schema Trade
CREATE TABLE Trade.StockTran
TransID BIGINT IDENTITY(1,1) PRIMARY KEY ,
INSERT INTO Trade.StockTran (StockTICKER,Qty,TransDate,TransTime)
Select * from Trade.StockTran
TransID StockTicker Qty TransDate TransTime -------------------- ----------- ----------- ---------- ---------------- 1 RAVI 20 2007-08-28 23:43:04.0972273 2 RAVI -5 2007-08-28 23:43:04.1128530
3) SMALLDATETIME : This is a data type that has the accuracy to 1 minute and useful for storing dates and time when the precision doesn't matter too much for example. The order booking date and time of a user. The range supported by this type of data type is 1990-01-01 through 2079-06-06 or January 1, 1900, through June 6, 2079 and time range between 00:00:00 through 23:59:59. The data type takes 4 fixed bytes to store the data.
4) DATETIME : This is a well known data type by most of us the date range supported is 01-01-1753 through 9999-12-31 or January 1, 1753, through December 31, 9999 and time range supported is 00:00:00 through 23:59:59.997. It takes 8 bytes to store the date/time data.
5) DATETIME2 : This is a new data type introduced in SQL Server 2008 and this date/time data type is introduced to store the high precision date and time data. The data type can be defined for variable lengths depending on the requirement. Please refer the following table for more information on the data type. This data type also follows the Gregorian Calendar and is not Day Light Saving Aware. The Time Zone can't be specified in this data type. This is still useful because it gives you a complete flexibility to store the date time data as per your requirement.
Create Table Trade.DateTest
DateID INT IDENTITY(1,1) PRIMARY KEY,
--Insert Today's Date
DECLARE @Now as DATETIME2(7)
SELECT @Now = SYSDATETIME()
INSERT INTO Trade.DateTest
--Insert Starting Range
SELECT @Now = '0001-01-01 00:00:00'
--Insert Ending Range
SELECT @Now = '9999-12-31 23:59:59.9999999'
SELECT * FROM Trade.DateTest
NOTE: The Matrix is transposed to fit in the blog space.
6) DATETIMEOFFSET : This is the new data type that is included in SQL Server 2008 and this data type is the most advanced in the league. We can store high precision date/ time with the Date Time Offset. We can't store the Time Zone like Eastern Time, Central Time etc. in the data type but can store the offset -5:00 for EST and -6:00 CST and so on. The data type is not Day light saving aware.
The date range is between 0001-01-01 and 9999-12-31 or January 1,1 A.D. through December 31, 9999 A.D. and the Time Range is between 00:00:00 and 23:59:59.9999999. The offset range is between -14:00 through +14:00. The precision of the data type can be set manually and it follows the Gregorian Calendar. For more details look at the following table.
Create Schema CorpIT
Create Table CorpIT.LoginCredentials
UserID INT IDENTITY(1,1) PRIMARY KEY,
domainname varchar(50) NOT NULL,
loginname varchar(50) NOT NULL,
creationDate DATETIMEOFFSET(7) NOT NULL
--INSERT A DATE VALUE WITH THE US CENTRAL TIME ZONE OFFSET
DECLARE @CreateDt CHAR(50)
SELECT @CreateDt = '2007-05-08 12:35:29.1234567 -6:00'
--INSERT A DATE VALUE WITH THE INDIAN TIME ZONE
SELECT @CreateDt = '2007-05-08 12:35:29.1234567 +5:30'
--INSERT A DATE VALUE WITH THE AUSTRALIAN TIME ZONE
SELECT @CreateDt = '2007-05-08 12:35:29.1234567 +10:00'