Beruflich Dokumente
Kultur Dokumente
Table of Contents
Working with the new DATE datatype in SQL Server 2008 .................................................... 1
Exercise 1 Exploring DATE and TIME datatypes.........................................................................................................2
Exercise 2 Working with the DATE/TIME datatypes ...................................................................................................6
Exercise 3 Modifying T-SQL to benefit from the date datatype ...................................................................................9
Working with the new DATE datatype in SQL Server 2008
In this lab you will identify the limitations of working with date and time data in
Scenario previous editions of SQL Server 2005. You will identify the new datatypes and
new functions that release with SQL Server 2008 and make modifications to
existing data by modifying database structures, and implementing date/time best
practices.
The introduction of these new datatypes allow you to better store and work with
date and time data, including multiple time zones and enhanced date calculations.
Besides these new datatypes a new set of date and time related functions are
introduced.
Note: This lab is tested with SQL Server 2008 codename ‘Katmai’ CTP 4. The
documentation and instructions in this lab is pre-release and is subject to change
in future release.
Page 1 of 13
Working with the new DATE datatype in SQL Server 2008
Exercise 1
Exploring DATE and TIME datatypes
Scenario
In this exercise, you will explore the new DATE and TIME datatypes in SQL Server 2008.
Note: Lab Scripts - In order to assist you completing the hands-on-labs a .sql script file for every exercise in this lab
has been created inside the virtual machine. The scripts for this lab can be found on the desktop under the SQL 2008
HOLs shortcut.
select * from sys.systypes where name like '%date%' or name like '%time%'
Page 2 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
Note that the following datatypes are new in SQL Server 2005: Datetime2,
datetimeoffset, date, time
4. Create a table with a. In the same query window, delete the previous statement(s).
the new datatypes b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
USE LABDB
GO
CREATE TABLE TBL_NewDatetimetypes (DateValue date, Timevalue Time,
DateTimeOffset datetimeoffset, Datetime2value datetime2)
5. Insert data in the a. In the same query window, delete the previous statement(s).
table b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Page 3 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
6. Understanding the Note: Based on the previous query results you should understand that the new
stored data datatypes in SQL Server support the following storage:
This will allow you for a more granular storage of date and time data in SQL Server
2008.
Page 4 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
7. Using the Note: Information - The datetimeoffset datatype allows you to store the timezone
datetimeoffset identifier together with the date value.
datatype
a. In the same query window, delete the previous statement(s).
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
select datepart(TZoffset,sysdatetimeoffset())
Note: Depending on the server timezone settings this will return the value in minutes
from the GMT standard time. This will be -420 for PST, 60 for GMT+1
9. Use the a. In the same query window, delete the previous statement(s).
SWITCHOFFSET b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
function to return a
by pressing ‘F5’
date in a different
timezone
select currenttime,SWITCHOFFSET ( currenttime,
datepart(TZoffset,sysdatetimeoffset())) as TimeinCurrentTimezone
from tbl_timezones
Page 5 of 13
Working with the new DATE datatype in SQL Server 2008
Exercise 2
Working with the DATE/TIME datatypes
Scenario
In this exercise you will identify some limitations of the datetime and smalldatetime datatype in previous editions of
SQL Server and you will take corrective actions to address those limitations.
USE LABDB
GO
if object_id('tbl_historicalEvents') is not null
drop table tbl_historicalEvents
Page 6 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
Note: The last statement will fail with the following error:
Note: This error occurs because the date value you are trying to insert falls outside
the scope of the datetime datatype. (01-01-1753 through 9999-12-31)
4. Recreate the table a. In the same query window, delete the previous statement(s).
using the new date b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
datatype
by pressing ‘F5’
USE LABDB
GO
if object_id('tbl_historicalEvents') is not null
drop table tbl_historicalEvents
Page 7 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
Note: You will now notice that the last insert runs successfully because of the date
datatype.
6. Close SQL Server a. Close SQL Server Management Studio without saving changes.
Management Studio
Page 8 of 13
Working with the new DATE datatype in SQL Server 2008
Exercise 3
Modifying T-SQL to benefit from the date datatype
Scenario
In this exercise you will modify an existing function to benefit from the date datatype range provided in SQL Server
2008.
You have a user-defined table valued function that calculates the interest value over a certain period in time starting
from the current date. The procedure excepts 3 parameters: Amount, Percentage, Years and returns a table that
shows you the amount of interest and the total amount including the interest value over the period of Years, starting
from the current date.
USE LABDB
GO
CREATE FUNCTION UDF_CALCULATE_INTEREST
(@Amount decimal (9,2), @Percentage decimal (9,2),
@Years int)
RETURNS
@tbl_InterestCalculation
TABLE (Startdate smalldatetime, Amount decimal (9,2),
Percentage Decimal(9,2), TotalAmountByYearEnd decimal(9,2), InterestAmount
decimal (9,2),
YearCounter int, EndDate smalldatetime)
as
begin
declare @loop tinyint
Page 9 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
declare @TotalAmountByYearEnd decimal (9,2)
declare @StartDate datetime
declare @Enddate datetime
set @StartDate = getdate()
set @TotalAmountbyYearEnd = 0
set @loop = 0
while @loop < @Years
begin
set @loop = @loop + 1
set @TotalAmountByYearEnd =
@Amount + (@Amount * @percentage)
set @Enddate = Dateadd(year,1,@Startdate)
insert into @tbl_InterestCalculation
values (@StartDate,@Amount,
@percentage,@TotalAmountByYearEnd,
@TotalAmountByYearEnd -
@Amount,@loop,@Enddate)
set @startdate = @Enddate
set @Amount = @TotalAmountByYearEnd
end
return
end
4. Rerun the query with a. In the same query window, delete the previous statement(s).
out of scope values b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Page 10 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
Note: This error occurs because the smalldatetime variable used in the function would
not allow the data conversion going to a data post 2079.
5. Modify the function Note: Information - By modifying the function to a date datatype you will address the
to apply the new date date conversion issues you had in the previous call to the function.
datatype
a. Click on ‘File | Open | File’ in the standard toolbar.
b. Browse to the SQL 2008 HOL ShortCut on the desktop and open the
CreateFunction.SQL script in the Date Time Data Folder. The following
content will display:
USE LABDB
GO
CREATE FUNCTION UDF_CALCULATE_INTEREST
(@Amount decimal (9,2), @Percentage decimal (9,2),
@Years int)
RETURNS
@tbl_InterestCalculation
TABLE (Startdate smalldatetime, Amount decimal (9,2),
Percentage Decimal(9,2), TotalAmountByYearEnd decimal(9,2), InterestAmount
decimal (9,2),
YearCounter int, EndDate smalldatetime)
as
begin
declare @loop tinyint
declare @TotalAmountByYearEnd decimal (9,2)
declare @StartDate datetime
declare @Enddate datetime
set @StartDate = getdate()
set @TotalAmountbyYearEnd = 0
set @loop = 0
while @loop < @Years
begin
set @loop = @loop + 1
set @TotalAmountByYearEnd =
@Amount + (@Amount * @percentage)
set @Enddate = Dateadd(year,1,@Startdate)
insert into @tbl_InterestCalculation
values (@StartDate,@Amount,
@percentage,@TotalAmountByYearEnd,
@TotalAmountByYearEnd -
Page 11 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
@Amount,@loop,@Enddate)
set @startdate = @Enddate
set @Amount = @TotalAmountByYearEnd
end
return
end
c. Modify the code to replace the smalldatetime and datetime variable declarations
by a date datatype and replace the create statement with an ALTER FUNCTION
statement. The code should display as listed:
USE LABDB
GO
ALTER FUNCTION UDF_CALCULATE_INTEREST
(@Amount decimal (9,2), @Percentage decimal (9,2),
@Years int)
RETURNS
@tbl_InterestCalculation
TABLE (Startdate DATE, Amount decimal (9,2),
Percentage Decimal(9,2), TotalAmountByYearEnd decimal(9,2), InterestAmount
decimal (9,2),
YearCounter int, EndDate DATE)
as
begin
declare @loop tinyint
declare @TotalAmountByYearEnd decimal (9,2)
declare @StartDate DATE
declare @Enddate DATE
set @StartDate = getdate()
set @TotalAmountbyYearEnd = 0
set @loop = 0
while @loop < @Years
begin
set @loop = @loop + 1
set @TotalAmountByYearEnd =
@Amount + (@Amount * @percentage)
set @Enddate = Dateadd(year,1,@Startdate)
insert into @tbl_InterestCalculation
values (@StartDate,@Amount,
@percentage,@TotalAmountByYearEnd,
@TotalAmountByYearEnd -
@Amount,@loop,@Enddate)
set @startdate = @Enddate
set @Amount = @TotalAmountByYearEnd
end
return
end
d. Execute the Create Function statement by clicking ‘Execute’ on the standard
Page 12 of 13
Working with the new DATE datatype in SQL Server 2008
Tasks Detailed Steps
toolbar or by pressing ‘F5’
6. Re-Run the query a. In the same query window, delete the previous statement(s).
with the previous out b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
of scope values
by pressing ‘F5’
The query will now execute successfully and display the result set for a period of
80 years.
7. You will display the a. In the same query window, delete the previous statement(s).
result if you would b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
invest $2000 over a
by pressing ‘F5’
period of 100 years
with an interest rate
of 1 percent select * from dbo.UDF_CALCULATE_INTEREST (500,0.01,100)
where YearCounter = 100
8. Close SQL Server a. Close SQL Server Management Studio without saving changes.
Management Studio
Page 13 of 13