时间如何比较大小

cj_c123 2010-03-31 04:14:26
字段2为时间函数 放的数据有5:00 6:30 22:00 21:00 等等

但是用以下查询的结果是

21:00
22:00
5:00
6:30

sql="select * from 表1 where 字段1='"&daynow&"' order by 字段2 asc"

看看出错到哪了

想要的结果是

5:00
6:30
21:00
22:00
...全文
497 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jwwyqs 2010-03-31
  • 打赏
  • 举报
回复
create table #tb1(dt varchar(10))
insert #tb1 select '21:00'
insert #tb1 select '22:00'
insert #tb1 select '5:00'
insert #tb1 select '6:30'

select * from #tb1 order by cast( replace(dt,':','.') as float) asc
htl258_Tony 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 cj_c123 的回复:]
字段2为时间函数 放的数据有5:00 6:30 22:00 21:00 等等

但是用以下查询的结果是

21:00
22:00
5:00
6:30

sql="select * from 表1 where 字段1='"&daynow&"' order by 字段2 asc"

看看出错到哪了

想要的结果是

5:00
6:30
21:00
2……
[/Quote]
--改为
sql="select * from 表1 where 字段1='"&daynow&"' order by cast(字段2 as datetime) asc"
--小F-- 2010-03-31
  • 打赏
  • 举报
回复
create table #tb1(dt varchar(10))
insert #tb1 select '21:00'
insert #tb1 select '22:00'
insert #tb1 select '5:00'
insert #tb1 select '6:30'

select * from #tb1 order by cast(dt as datetime)
/*dt
----------
5:00
6:30
21:00
22:00

(4 行受影响)
*/
永生天地 2010-03-31
  • 打赏
  • 举报
回复
字段2不是datetime类型。。。
水族杰纶 2010-03-31
  • 打赏
  • 举报
回复
create table tab(c1 varchar(10))
go
insert tab
select '21:00'
union select '22:00'
union select '5:00'
union select '6:30'
select *
from tab
order by right('00'+c1,5)
drop table tab
/*
c1
----------
5:00
6:30
21:00
22:00

(4 個資料列受到影響)

*/
leo_lesley 2010-03-31
  • 打赏
  • 举报
回复

create table tab(c1 varchar(10))
go
insert tab
select '21:00'
union select '22:00'
union select '5:00'
union select '6:30'


select * from tab order by cast(substring(c1,1,charindex(':',c1)-1) as int) asc

drop table tab
--小F-- 2010-03-31
  • 打赏
  • 举报
回复
你不是按照时间字段来查的..转换为时间字段就可以了
leo_lesley 2010-03-31
  • 打赏
  • 举报
回复
sql="select * from 表1 where 字段1='"&daynow&"' order by cast(substring(字段2,1,charindex(':',字段2)-1) as int) asc"
东那个升 2010-03-31
  • 打赏
  • 举报
回复
create table #tb1(dt varchar(10))
insert #tb1 select '21:00'
insert #tb1 select '22:00'
insert #tb1 select '5:00'
insert #tb1 select '6:30'


select * from #tb1 order by cast('1900-1-1 '+dt as datetime) desc

dt
----------
22:00
21:00
6:30
5:00

(4 行受影响)
ws_hgo 2010-03-31
  • 打赏
  • 举报
回复
21:00
22:00
5:00
6:30

这个结果已经查询出来了吗?

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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