日期转换问题,比较纠结,不安道理出牌

chuanzhang5687 2013-04-28 12:20:13


这是表 Hr_leave_form 里面的部分数据

这个表中的字段类型,start_date,end_date 为smalldatetime
start_time ,end_time 为字符串类型

下面是我的这段sql 语句
select *	
from hr_leave_form f
where user_id = 133
and delete_flag = 0
and convert(smalldatetime,start_date + ' ' + start_time) <= convert(smalldatetime,str(20130311) + ' 11:00')
and convert(smalldatetime,end_date + ' ' + end_time) >= convert(smalldatetime,str(20130311)+ ' 10:00')


这样执行的话,就会报

消息 295,级别 16,状态 3,第 1 行
将字符串转换为 smalldatetime 数据类型时失败。


但是我将图片上的数据 插入到我自己做的测试表中,查询这段语句是没问题的,
搞不明白这是为什么。

...全文
442 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
MrYangkang 2013-05-05
  • 打赏
  • 举报
回复
CONVERT(smalldatetime,convert(nvarchar(100),a,23)+' '+convert(nvarchar(5),convert(nvarchar(100),a,8))+b)
MrYangkang 2013-05-05
  • 打赏
  • 举报
回复
declare @tab table(a smalldatetime,b varchar(10)) insert into @tab values('20130429 10:00',' :00') --select a+b from @tab where ISDATE(b)=1 and --CONVERT(smalldatetime,convert(nvarchar(100),a,23)+' '+convert(nvarchar(5),convert(nvarchar(100),a,8))+b) <=CAST(getdate() as smalldatetime) select CONVERT(smalldatetime,convert(nvarchar(100),a,23)+' '+convert(nvarchar(5),convert(nvarchar(100),a,8))+b) from @tab where CONVERT(smalldatetime,convert(nvarchar(100),a,23)+' '+convert(nvarchar(5),convert(nvarchar(100),a,8))+b) <=CAST(getdate() as smalldatetime)
szm341 2013-05-05
  • 打赏
  • 举报
回复
正式环境数据量大或者数据分布不同导致where条件不能通过isdate或者是delete_flag = 0条件过滤掉不能转换的数据,执行计划表扫描的时候convert到了不能转换的数据就报错喽~ 试试看加个索引,或者简单一点用with先过滤数据后再转换查询呗
---涛声依旧--- 2013-05-05
  • 打赏
  • 举报
回复
楼主再查一下看看还有没有其他不是时间的数据,估计还有了 你的测试环境的数据应该跟正式的不一样了,一样的话肯定也行的
chuanzhang5687 2013-05-02
  • 打赏
  • 举报
回复
顶一下!!!!
chuanzhang5687 2013-04-30
  • 打赏
  • 举报
回复
引用 15 楼 Beirut 的回复:
[quote=引用 14 楼 Vidor 的回复:] [quote=引用 12 楼 Beirut 的回复:] [quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1[/quote] 优先级和执行计划都一回事了 判断相加后的和单独判断时间也一样的。[/quote] 小爱蝈蝈辛苦啦,发完贴看好下午搬家,没来得及看, 这种问题为啥很正常呢? 讲几个原因? 我也用相同的数据做测试表测试了,就没报错。为啥正式环境就报错了。 其他回答问题的,再自己看看题目
shoppo0505 2013-04-30
  • 打赏
  • 举报
回复
楼主,你的语句没有问题,我能想到的唯一解释,就是你有数据delete_flag = 0,但是时间列的数据还是不符合要求的,其他可能性几乎没有了。
黄_瓜 2013-04-30
  • 打赏
  • 举报
回复
引用 17 楼 tangguangqiang 的回复:
[quote=引用 15 楼 Beirut 的回复:] [quote=引用 14 楼 Vidor 的回复:] [quote=引用 12 楼 Beirut 的回复:] [quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1[/quote] 优先级和执行计划都一回事了 判断相加后的和单独判断时间也一样的。[/quote] 真的不行了。 下面是我测试的

declare @tab table(a smalldatetime,b varchar(10))
insert into @tab values('20130429 10:00',' :00')
select a+b from @tab  where   ISDATE(b)=1 and CONVERT(smalldatetime,a+b)<=CAST(getdate() as smalldatetime)
/*
(1 行受影响)
消息 295,级别 16,状态 3,第 4 行
将字符串转换为 smalldatetime 数据类型时失败。
*/
[/quote] 这种方式也不是很好使,也是会有可能出现错误的,比如这位写的例子 应该用case when 的方式去处理最恰当了 declare @tab table(a smalldatetime,b varchar(10)) insert into @tab values('2013-04-28 00:00:00',':00'),('2013-04-29 00:00:00','10:00') select * from @tab where CONVERT(smalldatetime,a+case when ISDATE(b)=1 then b else '00:00' end)<=CAST(getdate() as smalldatetime)
习惯性蹭分 2013-04-29
  • 打赏
  • 举报
回复
引用 15 楼 Beirut 的回复:
[quote=引用 14 楼 Vidor 的回复:] [quote=引用 12 楼 Beirut 的回复:] [quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1[/quote] 优先级和执行计划都一回事了 判断相加后的和单独判断时间也一样的。[/quote] 真的不行了。 下面是我测试的

declare @tab table(a smalldatetime,b varchar(10))
insert into @tab values('20130429 10:00',' :00')
select a+b from @tab  where   ISDATE(b)=1 and CONVERT(smalldatetime,a+b)<=CAST(getdate() as smalldatetime)
/*
(1 行受影响)
消息 295,级别 16,状态 3,第 4 行
将字符串转换为 smalldatetime 数据类型时失败。
*/
chuanzhang5687 2013-04-28
  • 打赏
  • 举报
回复
这数据里面有一个条数据 start_time 格式是错误的转换不了,我把它的delete_flag 设置为-1 查询条件里面 有 delete_flag = 0 好像这个条件没有什么作用,还是报错。
chuanzhang5687 2013-04-28
  • 打赏
  • 举报
回复
select *	 
from hr_leave_form f
where user_id = 133 
and delete_flag = 0			
and convert(smalldatetime,start_date + ' ' + start_time) <= convert(smalldatetime,str(20130311) +  ' 11:00')
and convert(smalldatetime,end_date + ' ' + end_time) >= convert(smalldatetime,str(20130311)+ ' 10:00')		

Vidor 2013-04-28
  • 打赏
  • 举报
回复
引用 15 楼 Beirut 的回复:
[quote=引用 14 楼 Vidor 的回复:] [quote=引用 12 楼 Beirut 的回复:] [quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1[/quote] 优先级和执行计划都一回事了 判断相加后的和单独判断时间也一样的。[/quote] 呵呵,那就一样吧,当我什么也没说。
黄_瓜 2013-04-28
  • 打赏
  • 举报
回复
引用 14 楼 Vidor 的回复:
[quote=引用 12 楼 Beirut 的回复:] [quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1[/quote] 优先级和执行计划都一回事了 判断相加后的和单独判断时间也一样的。
Vidor 2013-04-28
  • 打赏
  • 举报
回复
引用 12 楼 Beirut 的回复:
[quote=引用 11 楼 qujunchang 的回复:] [quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1[/quote] 你和楼主都被盗号了吧。 你描红的条件好像也没用,优先级问题,ISDATE(start_time)=1
daiyueqiang2045 2013-04-28
  • 打赏
  • 举报
回复
引用 11 楼 qujunchang 的回复:
[quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 神马意思
黄_瓜 2013-04-28
  • 打赏
  • 举报
回复
引用 11 楼 qujunchang 的回复:
[quote=引用 6 楼 daiyueqiang 的回复:] 依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
[/quote] 首先除了楼上我之外的蠢货都不认真看题 其次我这个蠢货写错了个东西 and ISDATE(smalldatetime,start_date + ' ' + start_time)=1 应该是 and ISDATE(start_date + ' ' + start_time)=1
曲军昌 2013-04-28
  • 打赏
  • 举报
回复
引用 6 楼 daiyueqiang 的回复:
依你现在给出的数据,第五行 start_time :00数据格式存在问题,导致convert失败 建议进一步检查或者规范你的start_time 以及end_time的数据的有效性
da21 2013-04-28
  • 打赏
  • 举报
回复
刚才的多了个括号 select * from hr_leave_form f where user_id = 133 and delete_flag = 0 and convert(smalldatetime,convert(varchar(10),start_date, 120) + ' ' + start_time,120) <= convert(smalldatetime,'2013-03-11' + ' 11:00',120) and convert(smalldatetime,convert(varchar(10),end_date, 120) + ' ' + end_time,120) >= convert(smalldatetime,'2013-03-11'+ ' 10:00',120)
da21 2013-04-28
  • 打赏
  • 举报
回复
select * from hr_leave_form f where user_id = 133 and delete_flag = 0 and convert(smalldatetime,convert(varchar(10),start_date, 120) + ' ' + start_time,120) <= convert(smalldatetime,'2013-03-11') + ' 11:00',120) and convert(smalldatetime,convert(varchar(10),end_date, 120) + ' ' + end_time,120) >= convert(smalldatetime,'2013-03-11'+ ' 10:00',120)
MrYangkang 2013-04-28
  • 打赏
  • 举报
回复
首先,你第五行数据应该是有问题 然后将,start_date、end_date 先格式化为年月日 如: convert(smalldatetime,start_date + ' ' + start_time) 改成 convert(smalldatetime,convert(varchar(100),start_date, 23) + ' ' + start_time)
加载更多回复(5)

34,594

社区成员

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

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