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'
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'
相关文章推荐
- SQL Server 2005中Query(查询)Date Time(日期时间)时select不出record的问题
- SQL Server 2005中Query(查询)Date Time(日期时间)
- mysql日期函数From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT(后者只能格式化标准日期格式,时间戳的不行)
- android中的ProgressDialog(进度),DatePickerDialog(日期选择)与TimePickerDialog(时间选择)
- LocalDate LocalTime LocalDateTime DateTimeFormatter Java 8 新出的时间(time)和日期(date)的API的使用详细实例
- SQL SERVER 2005中的日期时间类型
- 各种杂项组件(3)之--CalendarView(日历视图)、DatePicker/TimePicker(日期、时间选择器)、NumberPicker(数值选择器)
- sql server语句中日期时间格式化查询
- 编译的日期 和时间__DATE__ __TIME__
- Java日期时间(Date/Time)
- Qt学习(7)——Qt5中的日期(Date)和时间(Time)(1)
- SQL 综合运用(SQL Server 2005)(别名、时间处理、字段处理、联合查询……)
- 在SQL Server中获得不包含时间部分的日期(用DateName()就可以获得相应的年、月、日或 convert()函数)
- Mysql datatime 利用str_to_date字符串转换为日期 进行时间段查询
- AppFuse中日期时间(Date,Timestamp,Time)类型问题
- sql server语句中日期时间格式化查询
- Android——使用DatePicker和TimePicker显示当前日期和时间
- fortran_date_and_time:获取日期和时间
- JAVA处理日期(Date)时间(Time)以及相关类的介绍
- Mysql日期时间类型(DATE,DATETIME,TIMESTAMP)和函数及与linux系统时间之间的转换(UNIX_TIMESTAMP、FROM_UNIXTIME)