数据库中关于datetime、date 、time数据类型比较,及插入日期时间数据格式

USE [DemoDataBase]
GO
/****** Object:  Table [dbo].[tb_Time]    Script Date: 11/21/2013 14:26:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_Time](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [datetime_Name] [datetime] NULL,
 [date_Name] [date] NULL,
 [time_Name] [time](6) NULL,
 CONSTRAINT [PK_tb_Time] PRIMARY KEY CLUSTERED
(
 [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tb_Time] ON
INSERT [dbo].[tb_Time] ([id], [datetime_Name], [date_Name], [time_Name]) VALUES (1, CAST(0x0000A27D00E8D271 AS DateTime), CAST(0xD8370B00 AS Date), CAST(0x06B07489D70B0000 AS Time))
INSERT [dbo].[tb_Time] ([id], [datetime_Name], [date_Name], [time_Name]) VALUES (2, CAST(0x0000A27D00E98D4E AS DateTime), CAST(0x6B360B00 AS Date), CAST(0x068041BEFC0A0000 AS Time))
INSERT [dbo].[tb_Time] ([id], [datetime_Name], [date_Name], [time_Name]) VALUES (3, CAST(0x0000A27D00E9E98A AS DateTime), CAST(0x6B360B00 AS Date), CAST(0x068041BEFC0A0000 AS Time))
INSERT [dbo].[tb_Time] ([id], [datetime_Name], [date_Name], [time_Name]) VALUES (4, CAST(0x0000A27D00E9FF73 AS DateTime), CAST(0x6B360B00 AS Date), CAST(0x068041BEFC0A0000 AS Time))
SET IDENTITY_INSERT [dbo].[tb_Time] OFF

 

插入脚本:

insert into tb_time values(getdate(),getdate(),getdate())  --插入系统时间

insert into tb_time values(getdate(),'2012-11-21','13:06:30') --插入自定义格式时间

 

id datetime_Name date_Name time_Name

1 2013-11-21 14:07:40.750 2013-11-21 14:07:40.750000
2 2013-11-21 14:10:20.207 2012-11-21 13:06:30.000000
3 2013-11-21 14:11:38.913 2012-11-21 13:06:30.000000
4 2013-11-21 14:11:57.610 2012-11-21 13:06:30.000000

 

说明:

1)本表有id,datetime_Name,date_Name,time_Name 四个字段表示主键id,日期时间类型字段,日期类型字段,时间类型字段;

2)当插入getdate()系统函数的时候,如果是datetime类型的会转化显示日期时间数据:2013-11-21 14:07:40.750;如果是date类型的会转化为日期数据:2013-11-21 

如果是time类型的会转化为时间数据:14:07:40.750000。
3)还可以自定义时间插入,这时候以字符串的格式插入,数据格式必须和getdate()生成的系统时间格式一致。

4)在查询的时候可以将其转化为字符串格式的数据,便于between and查询。

5)当然,可以直接定义字符串类型的字段,在插入时间数据的时候转化为字符串插入,这样查询的时候就直接between and 查询。

你可能感兴趣的:(SqlServer2005,SqlServer2008)