我的数据库的时间字段用vchar(20)保存数据,我如何做时间段的比较

e3a326 2008-03-23 05:37:57
我希望查询一段时间内的数据
select Oper_name,Oper_time,Oper_thing from User_Log where Oper_time >= '2007-2-5 17:12:46' and Oper_time <= '2008-3-23 17:12:46' //法完成预期操作
应该如何处理了,谢谢了
...全文
100 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxmcxm 2008-03-23
  • 打赏
  • 举报
回复
将字段先转为日期时间字段再比较.如2楼所列.
dawugui 2008-03-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 e3a326 的帖子:]
我希望查询一段时间内的数据
select Oper_name,Oper_time,Oper_thing from User_Log where Oper_time >= '2007-2-5 17:12:46' and Oper_time <= '2008-3-23 17:12:46' //法完成预期操作
应该如何处理了,谢谢了
[/Quote]
如果是:'2007-02-05 17:12:46' ,'2008-03-23 17:12:46'这样的话,可以直接比较.
否则要先转换为datetime型.

select Oper_name,Oper_time,Oper_thing from User_Log where cast(Oper_time as datetime) >= '2007-02-05 17:12:46' and Oper_time <= '2008-03-23 17:12:46'
Limpire 2008-03-23
  • 打赏
  • 举报
回复
单单是字符比较:

'2008-3-23 17:12:46' > '2008-12-23 17:12:46'

格式很重要,包括数据的格式和条件的格式都要遵照:yyyy-mm-dd hh:mm:ss

if '2008-03-23 17:12:46' < '2008-12-23 17:12:46' print '<'

e3a326 2008-03-23
  • 打赏
  • 举报
回复
我是在帮客户抄袭一个小系统,那个系统设计就这样,为了兼容而已。
谢谢楼上的,以及更上面的。
Limpire 2008-03-23
  • 打赏
  • 举报
回复
真搞不懂为什么要用varchar(20)来保存时间

占空间:datetime 8,smalldatetime 4
有效性验证:自己验证,datetime系统验证
比较:还得转换为时间来比较,除非严格按照这种格式:yyyy-mm-dd hh:mm:ss
Limpire 2008-03-23
  • 打赏
  • 举报
回复
select Oper_name,Oper_time,Oper_thing from User_Log where convert(datetime,Oper_time) between '2007-2-5 17:12:46' and '2008-3-23 17:12:46'
wzy_love_sly 2008-03-23
  • 打赏
  • 举报
回复
这就是时间段啊

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧