`
stta04
  • 浏览: 112524 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

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

阅读更多

在浏览本文之前请保证安装好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'

分享到:
评论

相关推荐

    Microsoft SQL Server 2016: A Beginner’s Guide, 6th Edition

    Get up and running on Microsoft SQL Server 2016 in no time with help from this thoroughly revised, practical resource. The book offers thorough coverage of SQL management and development and features ...

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    微软内部资料-SQL性能优化5

    With a multitable query, SQL Server must sometimes search a table multiple times so each page is scanned much more than once. Having useful indexes speeds up finding individual rows in a table, as ...

    SQL语法大全

    SQL语法大全 SQL语法大全 1. ASP与Access数据库连接: dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access ...

    Dimeric.Virtual.Database.

    For example, the macro for the database‘s current date and time would be CURRENT for the Informix dialect and getdate() for the SQL Server dialect. In VDB you would use the [now()] function. If you ...

    unidac 7.4.11 pro for RAD Studio 10.3

    SQLServer data provider QuoteNames option in TUniLoader to escape field names is added Bug with mapping a TEXT field to ftWideMemo in Delphi is fixed Bug with SQL statements containing a CONTAINS ...

    Devart UniDAC 7.4.11 Professional for RAD Studio 10.3 Rio

    SQLServer data provider QuoteNames option in TUniLoader to escape field names is added Bug with mapping a TEXT field to ftWideMemo in Delphi is fixed Bug with SQL statements containing a CONTAINS ...

    UnidacPro8.0.1Rio.rar

    setting a query for the Query component is fixed DBF data provider ...query result when using local indexes is fixed Bug with mapping DATE fields is fixed BigCommerce data provider OAuth authentication ...

    UnidacProv8.0.1Tokyo.rar

    setting a query for the Query component is fixed DBF data provider ...query result when using local indexes is fixed Bug with mapping DATE fields is fixed BigCommerce data provider OAuth authentication ...

    Oracle9i的init.ora参数中文说明

    说明: 指定一对值 (UTC,TZD), 设置 TIME WITH TIME ZONE 数据类型的默认值, 该数据类型包含 HOUR, MINUTE, SECOND, TIMEZONE_HOUR 和 TIMEZONE_MINUTE 这几个日期时间字段。UTC 是世界时而 TZD 是当地时区。 语法: ...

    AppFramework_V1.0_New

    代码生成器,ORMap,支持Oracle/SqlServer/MSAccess,性能大大优于iBatisNet,终身免费,随意使用。 =========== 软件说明: 1.1 引言 约有90%的企业信息化管理系统基于数据库实现,这类系统中又有超过...

    AppFramework数据库访问组件_代码生成插件_V1.1.rar

    内含代码生成器,支持Oracle/SqlServer/MSAccess,ORMap性能大大优于iBatisNet,终身免费无限制使用,绝无任何版权问题。 =========== 软件说明: 1.1 引言 约有90%的企业信息化管理系统基于数据库...

    AppFramework_V1.0

    最明显的就是类似于LAST_UPDATE_TIME了,通常为了保证这个字段的一致性,通常在插入新记录时采用当前数据库时间作为字段值。但IBatisNet接收的实体类对象属性都是普通C#类型,并不具备传入表达式的能力。如果采用...

    FastReport.v4.9.81 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版

    FastReport.v4.9.81 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版 delphi2010中文完美支持。 D2010安装必读 delphi2010使用者安装时,请将res\frccD14.exe更名名为frcc.exe frccD14.exe 是专门的...

    ZendFramework中文文档

    10.4.2. 同一表中查询多列数据 10.4.3. 多表联合查询 10.4.4. WHERE条件 10.4.5. GROUP BY分句 10.4.6. HAVING 条件 10.4.7. ORDER BY 分句 10.4.8. 通过总数和偏移量进行LIMIT限制 10.4.9. 通过页数和总数...

    UniDAC 7.1.4

    Bug with date and time fractional seconds precision when DescribeParams = True is fixed MySQL data provider Azure Database for MySQL is supported JSON data type is supported InterBase data provider...

    【分布式事务----LCN】LCN原理及使用方式.docx

    - 该模式由于每次执行SQL之前需要先查询影响数据,因此相比LCN模式消耗资源与时间要多。 - 该模式不会占用数据库的连接资源。 二、原理 核心步骤 1.创建事务组 是指在事务发起方开始执行业务代码之前先调用...

    高性能服务框架ZYS.zip

    var_dump(distributed::getInstance()-&gt;query($sql)); //文件同步(不用安装rsync notify就可以实现文件同步,并且是触发式的占用很小的资源,调用sendfile零复制) $dir_pre=MYPATH.'/public/uploads/'; if(!is...

    jpivot学习总结.doc

    uniqueMembers 该属性用于优化产生的 SQL ,如果你知道这个级别和其父级别交叉后的值或者是维度表中给定的级别所有的值是唯一的,那么就可以设置该值为 true ,否则为 false 。 levelType 该 Level 的类型,默认...

Global site tag (gtag.js) - Google Analytics