您的位置:首页 > 产品设计 > UI/UE

SQL Server 2005中Query(查询)Date Time(日期时间)

2012-01-12 11:44 417 查看
在浏览本文之前请保证安装好SQL Server 2005。

Introduction

本文主要讲述在SQL Server 2005中Query(查询)Date Time(日期时间)会遇到的问题。

Section 1 - Problem

现在打算select FundHouse table中LastUpdateDate column中date为2008-5-21的records。

在SQL Server 2005中open FundHouse这个table可以看到在LastUpdateDate column下有以下值:

2008-5-21 19:38:36

2008-5-21 19:42:40

2008-5-21 19:43:45

2008-5-23 2:23:03

2008-5-23 2:34:43

New一个Query去select,尝试了以下几种形式:

select * from FundHouse where LastUpdateDate like '2008-5-21'

select * from FundHouse where LastUpdateDate like '2008-5-21%'

select * from FundHouse where LastUpdateDate like 2008-5-21

select * from FundHouse where LastUpdateDate like 2008-5-21%

select * from FundHouse where LastUpdateDate like '2008-5-21 19:38:36'

select * from FundHouse where LastUpdateDate = '2008-5-21 19:38:36'

select * from FundHouse where LastUpdateDate = '2008-5-21'

但第1~6种形式是不能正确把records select出来,只有第7种能正确select出来,这表示这不能用like和%来通配所有时间值。

从以上第5,6种形式可以看到即使具体到秒也不能把record select出来。

Section 2 - Reason

SQL Server 2005的datetime type存储的实际数据是精确到毫秒级的,但现在不确定是否与此有关。

Section 3 - Solution

可以用"<" or ">"或者"between ? and ?"设定范围来取值。

如用prepareStatement的话like ?这个问号不能set一个Date的date type它,因为在SQL Server 2005上测试,如以下两种写法都会select出不对的result:

select * from FundHouse where LastUpdateDate > 2008-5-21

select * from FundHouse where LastUpdateDate < 2008-5-21

经试验,只能用String date type来表示这个时间才能select出正确result,如下这样:

select * from FundHouse where LastUpdateDate > '2008-5-21'

select * from FundHouse where LastUpdateDate < '2008-5-21'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐