热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

请问大侠怎么用SQL筛选几个固定的时间的记录?谢谢

我用的是sqlserver表tab1------------------------------------------date_time
我用的是sql server

 表 tab1
------------------------------------------
   date_time                  action
   2009-6-20 09:00:00         坐车出去
   2009-6-20 10:30:00         唱歌
   2009-6-20 12:00:00         吃饭
   2009-6-20 12:30           看报纸
   2009-6-20 14:00           回家
   2009-6-21 09:00:00         上班
   2009-6-21 10:00:00         写报告
   2009-6-21 10:30:00         开会

我想筛选每天 09:00:00  和 10:30:00的记录,不比较日期,只比较时间部分。
想要结果集合是:

   date_time                  action
   2009-6-20 09:00:00         坐车出去
   2009-6-20 10:30:00         唱歌
   2009-6-21 09:00:00         上班
   2009-6-21 10:30:00         开会



   
谢谢各位大侠

22 个解决方案

#1


select * from tab1 where convert(varchar(10),date_time,108) between '09:00:00' and '10:30:00'

#2



select * from tab1 where convert(nvarchar(10),date_time,108) between '09:00:00' and '10:30:00'

#3



--理解错了:
select * from tab1 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'

#4



select * from tab1 where convert(varchar(10),date_time,108) in ('09:00:00','10:30:00')

这样还行啊?

#5


select * from tab1 
where substring(date_time,12,8) between '09:00:00' and '10:30:00'

#6


select * from tab1 
where substring(date_time,12,8) = '09:00:00' or substring(date_time,12,8)='10:30:00'

#7


select convert(varchar(10),getdate(),108) 
--10:06:46 当前时间,这个是转换datetime为时间字符串的函数

#8


只要整点的记录,09:00:00  和 10:30:00的记录,
而不是 要09:00:00 到 10:30:00之间的记录。

#9


select * from tab1 
where substring(date_time,12,8) between '09:00:00' and '10:30:00'

#10



---------------------------------
--  Author: htl258(Tony)
--  Date  : 2009-07-29 09:35:13
---------------------------------
--> 生成测试数据表:tab1

If not object_id('[tab1]') is null
Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
--Select * from tab1

-->SQL查询如下:
select * 
from tab1 
where cast(convert(varchar,[date_time],8) as datetime) between '09:00' and '10:30'
/*
date_time               action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:00:00.000 写报告
2009-06-21 10:30:00.000 开会

(5 行受影响)
*/

#11


引用 8 楼 cnlmgsoft 的回复:
只要整点的记录,09:00:00  和 10:30:00的记录,
而不是 要09:00:00 到 10:30:00之间的记录。

select * from tab1 
where substring(date_time,12,8) = '09:00:00' or substring(date_time,12,8)='10:30:00'

select * from tab1
 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'


#12


select * from tab1 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'



#13


date_time 是个“日期时间” 字段

#14


select * from tab1 
where substring(date_time,12,8) ='09:00:00' or substring(date_time,12,8) ='10:30:00'

#15


dingding

#16



---------------------------------
--  Author: htl258(Tony)
--  Date  : 2009-07-29 09:35:13
---------------------------------
--> 生成测试数据表:tab1

If not object_id('[tab1]') is null
Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
--Select * from tab1

-->SQL查询如下:
select * 
from tab1 
where cast(convert(varchar,[date_time],8) as datetime) in('09:00','10:30')
/*
date_time               action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:30:00.000 开会

(4 行受影响)
*/
如果只取9:00与10:30两个时间,这样.

#17


If not object_id('[tab1]') is null
    Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
select * from tab1
 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'
date_time               action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:30:00.000 开会

数据一用

#18


多谢,解决了,用这个可以
CONVERT(varchar(10), HL_DT_Design, 108)

#19



select * from tab1 where date_time like'%09:00:00%' or date_time  like'%10:30:00'

#20



declare @tabl table (date_time datetime,action nvarchar(20))
insert into @tabl select '2009-6-20 09:00:00','坐车出去'
      union all  select '2009-6-20 10:30:00','唱歌'
      union all  select '2009-6-20 12:00:00','吃饭'
      union all  select '2009-6-20 12:30:00','看报纸'
      union all  select '2009-6-20 14:00:00','回家'
      union all  select '2009-6-21 09:00:00','上班'
      union all  select '2009-6-21 10:00:00','写报告'
      union all  select '2009-6-21 10:30:00','开会'
select * from @tabl where CONVERT(nvarchar(10),date_time ,108) between '09:00:00' and '10:30:00'

date_time               action
----------------------- --------------------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:00:00.000 写报告
2009-06-21 10:30:00.000 开会

(5 行受影响)

#21


 

declare @tab1 table (date_time varchar(32),action nvarchar(8))
insert into @tab1 select
  '2009-6-20 09:00:00',        '坐车出去' union all select
  '2009-6-20 10:30:00',        '唱歌 ' union all select
  '2009-6-20 12:00:00',        '吃饭' union all select 
  '2009-6-20 12:30',          '看报纸' union all select 
  '2009-6-20 14:00',          '回家' union all select 
  '2009-6-21 09:00:00',        '上班' union all select 
  '2009-6-21 10:00:00',        '写报告' union all select 
  '2009-6-21 10:30:00',        '开会'
--------方法1-----------------------------------------------------------
select date_time,action from @tab1 where substring(date_time,charindex(' ',date_time)+1,8) in ('09:00:00','10:30:00')
--------方法2-----------------------------------------------------------
select date_time,action from @tab1 where (date_time like '%09:00:00') or (date_time like '%10:30:00')

#22


select
							     

推荐阅读
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • NotSupportedException无法将类型“System.DateTime”强制转换为类型“System.Object”
    本文介绍了在使用LINQ to Entities时出现的NotSupportedException异常,该异常是由于无法将类型“System.DateTime”强制转换为类型“System.Object”所导致的。同时还介绍了相关的错误信息和解决方法。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • 本文由编程笔记小编整理,主要介绍了使用Junit和黄瓜进行自动化测试中步骤缺失的问题。文章首先介绍了使用cucumber和Junit创建Runner类的代码,然后详细说明了黄瓜功能中的步骤和Steps类的实现。本文对于需要使用Junit和黄瓜进行自动化测试的开发者具有一定的参考价值。摘要长度:187字。 ... [详细]
  • Java学习笔记之使用反射+泛型构建通用DAO
    本文介绍了使用反射和泛型构建通用DAO的方法,通过减少代码冗余度来提高开发效率。通过示例说明了如何使用反射和泛型来实现对不同表的相同操作,从而避免重复编写相似的代码。该方法可以在Java学习中起到较大的帮助作用。 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
author-avatar
初始化
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有