技术开发 频道

SQL Server 2008使用日期时间数据类型

【IT168 技术文档】
SQL Server 2008 日期/时间数据类型

目录
准备工作 1
练习1:浏览日期与时间数据类型 2
练习 2:处理日期和时间数据类型 6
准备工作
预计完成本实验所需的时间
40 分钟
目标
在完成本实验后,您将可以:
处理SQL Server 2008当中的日期/时间数据类型

先决条件
在完成本实验前,您必须具有:
编写Transact-SQL 脚本与使用SQL Server Management Studio的相关经验。

实验场景

在本实验当中,您将会看到在SQL Server 2005当中,处理日期与时间所面临的问题与缺陷。您将会使用SQL Server 2008中发布的新的数据类型和新的功能,通过修改数据库架构来修改现有数据,并实现日期/时间数据类型的非常好的实验。

虚拟机环境

从开始菜单或桌面上启用Microsoft Virtual PC 。如果Virtual PC 控制台没有启用,请查看系统托盘,然后双击系统托盘当中的Microsoft Virtual PC 。
选择Sql08 然后点击Start。

在虚拟机运行起来后,可以通过点击右Alt+Del 来向虚拟机发送一个Ctrl+Alt+Del 命令。
在登录窗口中,输入以下信息:
User name: administrator
Password: password01!
 

练习1:浏览日期与时间数据类型

在SQL Server 2008 中,引入了新的日期与时间数据类型。
这些新的数据类型的引入,可以帮助您更好的存储与处理与日期和时间相关的数据,包括多时区的支持,增强的日期计算功能。
这些新的数据类型包括:
• datetime2
• date
• time
• datetimeoffset

启动 SQL Server Management Studio

点击Start | All Programs | Microsoft SQL Server 2008 | SQL Management Studio  ,启动SQL Server Management Studio。
在Connect to Server对话框中输入下列信息,然后点击 Connect 按钮:
Server type: Database Engine
Server name: (local)
Authentication: Windows Authentication

点击File | Open | File。
打开C:\SQLHOLS 文件夹,然后打开Date Time Datatype 目录当中的Labscript.sql 脚本文件。

创建实验数据库并浏览数据类型
查看并选中 下列代码并点击Execute:
CREATE DATABASE LABDB
GO
查看并 选中 下列代码并点击Execute:
select * from sys.systypes where name like '%date%' or name like '%time%'

查看并 选中 下列代码并点击Execute:

USE LABDB
GO
CREATE TABLE TBL_NewDatetimetypes (DateValue date, Timevalue Time, DateTimeOffset datetimeoffset, Datetime2value datetime2)

查看并 选中 下列代码并点击Execute:

INSERT  INTO TBL_NewDatetimetypes values (SYSDATETIME(),SYSDATETIME(),SYSDATETIMEOFFSET(),SYSUTCDATETIME())

注意: 上面的语句使用了下面几个函数:

SYSDATETIME – 获取当前数据库系统的时间戳,并返回一个datetime2(7) 的值。数据库时间中不包含时区信息。这个值是从当前
SQL Server 实例所运行的操作系统的时间来获得的。

SYSDATETIMEOFFSET - 获取当前数据库系统的时间戳,并返回一个datetime2(7) 的值。这个值是从当前SQL Server 实例所运行的操作系统的时间来获得的。SYSDATETIME 和 SYSUTCDATETIME 比GETDATE与GETUTCDATE在秒上拥有更加细小的精确度。SYSDATETIMEOFFSET 包括了系统的时区信息。SYSDATETIME、SYSUTCDATETIME和SYSDATETIMEOFFSET可以为任意日期与时间类型赋值。

SYSUTCDATETIME - 获取当前数据库系统的时间戳,并返回一个datetime 的值。数据库时间中不包含时区信息。这个值表示当前的UTC时间(Coordinated Universal Time)。This value represents the current UTC time(Coordinated Universal Time)。 这个值是从当前SQL Server 实例所运行的操作系统的时间来获得的。

查看并 选中 下列代码并点击Execute:

SELECT * FROM TBL_NewDatetimetypes

理解存储的数据
基于前面的查询结果,您应当可以理解到,SQL Server 中新的数据类型可以支持下面一些存储方式:
查看并 选中 下列代码并点击Execute:

USE LABDB
Create table tbl_timezones (Entryid int identity(1,1),
                            Currenttime datetimeoffset)
GO
insert into tbl_timezones (currenttime)
values ('1998-09-20 7:45:50.71345 -5:00')
insert into tbl_timezones (currenttime)
values ('1956-01-27 6:45:50.00000 -3:00')
insert into tbl_timezones (currenttime)
values ('1972-12-18 7:45:50.71345 +1:00')
insert into tbl_timezones (currenttime)
values ('2005-01-20 7:12:50.71345 +9:00')
insert into tbl_timezones (currenttime)
values ('2005-01-20 01:00:00.00000 +4:00')

注意: datetimeoffset 数据类型可以允许您存储时区信息。
查看并 选中 下列代码并点击Execute:

select datepart(TZoffset,sysdatetimeoffset())

注意: 取决于服务器的时区设置,它将会返回与GMT标准时间相差的分钟数。
如PST将会是-420,而GMT+1则是60.
DATEPART 函数已经被扩展为从一个已知的时区返回时区的值,TZoffset值返回一个数字,表示与GMT标准时间相差的分钟数。因此,GMT + 8) 将返回的值为480。
查看并 选中 下列代码并点击Execute:

select currenttime,SWITCHOFFSET ( currenttime, datepart(TZoffset,sysdatetimeoffset())) as TimeinCurrentTimezone
from tbl_timezones)



 

练习 2:处理日期和时间数据类型

在本练习中,您将看到以前SQL Server版本当中,datetime与smalldatetime数据类型的一些问题,然后您将会采取一些行动来解决这些问题。

点击Start | All Programs | Microsoft SQL Server 2008 | SQL Management Studio  ,启动SQL Server Management Studio。
在Connect to Server对话框中输入下列信息,然后点击 Connect 按钮:

Server type: Database Engine
Server name: (local)
Authentication: Windows Authentication

创建一个数据表来处理数据类型
查看并 选中 下列代码并点击Execute:

USE LABDB
GO
if object_id('tbl_historicalEvents') is not null
drop table tbl_historicalEvents

create table tbl_historicalEvents (EventID int IDENTITY (1,1),
                  Eventname
nvarchar(200), Eventdate datetime)

查看并 选中 下列代码并点击Execute:

insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Birthday of Wolfgang Amadeus Mozart', '01/27/1756')
insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Universal declaration of Human Rights', '12/10/1948')
insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Birthday of Johan Sebastian Bach', '03/21/1685')

注意: 最后一条语句将会出现如下错误

Msg 242, Level 16, State 3, Line 17
The conversion
of a varchar data type to a datetime data type resulted in an out-of-range value.

注意: 发生这个错误的主要原因是,您希望插入到datetime数据类型当中的值超出了可以接受的范围 (从01-01-1753 到 9999-12-31)
查看并 选中 下列代码并点击Execute:

USE LABDB
GO
if object_id('tbl_historicalEvents') is not null
drop table tbl_historicalEvents

create table tbl_historicalEvents (EventID int IDENTITY (1,1),
                  Eventname
nvarchar(200),
                  Eventdate date)

查看并 选中 下列代码并点击Execute:

insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Birthday of Wolfgang Amadeus Mozart', '01/27/1756')
insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Universal declaration of Human Rights', '12/10/1948')
insert into tbl_historicalEvents (Eventname, Eventdate)
values ('Birthday of Johan Sebastian Bach', '03/21/1685')

注意: 您现在将发现最后一个INSERT语句将可以正常执行,因为使用了date 数据类型。
关闭所有应用程序并不要保存所有更改。
关闭Virtual PC 并不要保存更改。
 

0
相关文章