相同时间不同表达方式产生的不同结果?

claro 2008-12-06 03:32:26
--test data
If object_id('#') is not NULL
drop table #
Go
--建立测试表
Create table # (shijian datetime,ID int IDENTITY)
Go
--录入测试数据
Insert into #
select getdate()-340 Union ALL --录入2008-1-1
select getdate()-(340-31) Union ALL --录入2008-2-1
select getdate()-(340-31-29) Union ALL --录入2008-3-1
select getdate()-(340-31-29-31) Union ALL --录入2008-4-1
select getdate()-(340-31-29-31-30) Union ALL --录入2008-5-1
select getdate()-(340-31-29-31-30-31) Union ALL --录入2008-6-1
select getdate()-(340-31-29-31-30-31-30) Union ALL --录入2008-7-1
select getdate()-(340-31-29-31-30-31-30-31) Union ALL --录入2008-8-1
select getdate()-(340-31-29-31-30-31-30-31-31) Union ALL --录入2008-9-1
select getdate()-(340-31-29-31-30-31-30-31-31-30) Union ALL --录入2008-10-1
select getdate()-(340-31-29-31-30-31-30-31-31-30-31) Union ALL --录入2008-11-1
select getdate()-(340-31-29-31-30-31-30-31-31-30-31-30) --录入2008-12-1
--select * from #
--测试不同语句显示结果
select *
from #
where convert(varchar(100),shijian,23) >= '2008-01-01'
/*
shijian ID
2008-01-01 15:25:20.030 1
2008-02-01 15:25:20.030 2
2008-03-01 15:25:20.030 3
2008-04-01 15:25:20.030 4
2008-05-01 15:25:20.030 5
2008-06-01 15:25:20.030 6
2008-07-01 15:25:20.030 7
2008-08-01 15:25:20.030 8
2008-09-01 15:25:20.030 9
2008-10-01 15:25:20.030 10
2008-11-01 15:25:20.030 11
2008-12-01 15:25:20.030 12
*/
select *
from #
where convert(varchar(100),shijian,23) >= '2008-1-1'
/*
shijian ID
2008-11-01 15:25:20.030 11
2008-12-01 15:25:20.030 12
*/
...全文
115 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
claro 2008-12-08
  • 打赏
  • 举报
回复
谢谢
-狙击手- 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Yang_ 的回复:]
where convert(varchar(100),shijian,23) >= '2008-01-01'
where convert(varchar(100),shijian,23) >= '2008-1-1'
都是字符串比较

要想日期比较不要转换
where shijian >= '2008-01-01'
where shijian >= '2008-1-1'
结果一样的
[/Quote]

、、
ouyang156 2008-12-06
  • 打赏
  • 举报
回复
引用 8 楼 Yang_ 的回复:
where convert(varchar(100),shijian,23) >= '2008-01-01'
where convert(varchar(100),shijian,23) >= '2008-1-1'
都是字符串比较

要想日期比较不要转换
where shijian >= '2008-01-01'
where shijian >= '2008-1-1'
结果一样的






支持
rucypli 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Yang_ 的回复:]
where convert(varchar(100),shijian,23) >= '2008-01-01'
where convert(varchar(100),shijian,23) >= '2008-1-1'
都是字符串比较

要想日期比较不要转换
where shijian >= '2008-01-01'
where shijian >= '2008-1-1'
结果一样的
[/Quote]



Yang_ 2008-12-06
  • 打赏
  • 举报
回复
where convert(varchar(100),shijian,23) >= '2008-01-01'
where convert(varchar(100),shijian,23) >= '2008-1-1'
都是字符串比较

要想日期比较不要转换
where shijian >= '2008-01-01'
where shijian >= '2008-1-1'
结果一样的
CN_SQL 2008-12-06
  • 打赏
  • 举报
回复

use tempdb
go

declare @s varchar(20)
declare @s1 varchar(20)
declare @s2 varchar(20)


set @s = '2008-1-1'
set @s1 = '2008-10-1'
set @s2 = '2008-11-1'

if @s1 >= @s
print '"2008-10-1" >= "2008-1-"'
else
print '"2008-10-1" < "2008-1-"'

if @s2 >= @s
print '"2008-11-1" >= "2008-1-"'
else
print '"2008-11-1" < "2008-1-"'

/**
"2008-10-1" < "2008-1-"
"2008-11-1" >= "2008-1-"

**/

执行一下,看看结果.
CN_SQL 2008-12-06
  • 打赏
  • 举报
回复
其实看看执行计划就知道,在处理的时候,SQL SERVER是要做一个隐式转换的.
rucypli 2008-12-06
  • 打赏
  • 举报
回复
'2008-1-1'
代表'2008-10-10'?
CN_SQL 2008-12-06
  • 打赏
  • 举报
回复
这个问题其实是很多人忽视的问题,这样写SQL 语句,其实是在比较字符,而不是时间.
Andy__Huang 2008-12-06
  • 打赏
  • 举报
回复
你看上面的结果,不论字符串多长,从第一个字符开始比较,如果一个字符串的第一个字符已经比另一个字符串的第一个字符大,那么结果就出来了(第一个字符串大)
上面你已经用convert()函数转换它为字符串了,
所它比较的是字符串,而不是比较日期
Andy__Huang 2008-12-06
  • 打赏
  • 举报
回复
字符串比较从第一个字符开始比较

if '1'>'0999999'
print 'aaaa'
else
print 'bbbb'

结果:
aaaa
claro 2008-12-06
  • 打赏
  • 举报
回复
一般都会录入1,不会录入01吧?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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