我有一个Message表,其中begintime字段(开始时间),endtime字段(结束时间)要求查询出在这世间范围内的所有数据
解决方案 »
- sql 日期拆分
- 将查询条件中的某一列改变数据类型???
- 如果准确判断新建的索引是否可以提高查询效率???
- 关于两个表的数据连接问题
- [调查]数据量多大?是存储过程分页吗?性能怎么样?
- 求助:关于利用游标获取表名问题
- 熟悉分析数据库错误的请进
- update iteminf set (select image1 from iteminf)=(select image1 from iteminf2 where model_no=iteminf.model_no)
- 如何在DB2中创建存储过程
- 快一年没来这里了~!!!!!!!!!!!来给大家丢分,呵呵
- 如何设计产品表结构啊
- 关于数据库迁移后查询权限的问题
seletc * from TB where begintime>'2009-05-01 00:00:00'
and endtime<'2009-05-08 00:00:00'
from Message
where begintime>'2009-05-01' and endtime<'2009-05-08'
--查5月1日到5月7日的记录
select *
from Message
where begintime<='2009-05-08' or endtime>='2009-05-01'
樓主的數據中應該是每一條記錄中的 begintime小於endtime吧.如果是這樣的話.那就用上面的語句
只要開始時間 小於 最後一天 那麼它對應的結束時間肯定大於最後一天, 這條記錄應該算在這段時間吧.
同理 只要結束時間 大於 第一天,那麼它對應的開始時間肯定小於第一天, 這條記錄應該也算在這段時間吧.
-------------------
2009-05-08 09
這樣就把分鐘,秒取掉了.
and endtime<'2009-5-8'
((@begintime>=begintime and @begintime<=endtime) or (@endtime>=begintime and @endtime<=endtime)
or (begintime>=@begintime and begintime<=@endtime) or (endtime>=@begintime and endtime<=@endtime))
那你得先转化成字符类型,才可以。
你可以参考一下这个:--*******************************************************************************
-- 日期转换参数大全
--********************************************************************************
select CONVERT(varchar,getdate(),120)
--2009-03-15 15:10:02select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
--20090315151201select CONVERT(varchar(12) , getdate(), 111)
--2009/03/15select CONVERT(varchar(12) , getdate(), 112)
--20090315select CONVERT(varchar(12) , getdate(), 102)
--2009.03.15select CONVERT(varchar(12) , getdate(), 108)
--15:13:26其它我不常用的日期格式转换方法:select CONVERT(varchar(12) , getdate(), 101 )
--03/15/2009select CONVERT(varchar(12) , getdate(), 103 )
--15/03/2009select CONVERT(varchar(12) , getdate(), 104 )
--15.03.2009select CONVERT(varchar(12) , getdate(), 105 )
--15-03-2009select CONVERT(varchar(12) , getdate(), 106 )
--15 03 2009select CONVERT(varchar(12) , getdate(), 107 )
-- 15, 2009select CONVERT(varchar(12) , getdate(), 109 )
--03 15 2009 select CONVERT(varchar(12) , getdate(), 110 )
--03-15-2009select CONVERT(varchar(11) , getdate(), 113 )
--15 03 2009 select CONVERT(varchar(12) , getdate(), 114)
--15:15:55:717