SQL Server错误从字符串转换日期和/或时间时转换失败 [英] SQL Server error Conversion failed when converting date and/or time from character string

查看:4667
本文介绍了SQL Server错误从字符串转换日期和/或时间时转换失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当iam尝试执行此查询时,它返回sql server错误从字符串转换日期和/或时间时转换失败





when iam trying to execute this query it return sql server error Conversion failed when converting date and/or time from character string


select DATEDIFF(HOUR,CONVERT(varchar(19),inputDate),CONVERT(varchar(19),startingWork)) from TICKET_LOG




17-10-2015 11:30:51 and 12/4/2014 8:38:00 A string





错误是什么?任何人都可以帮我解决问题??



what is the error ? anyone can help me to solve the issue ??

推荐答案

你只能对某些数据类型使用某些操作。你不能约会varchars。



如果你的输入类型是日期类型,那么不要转换它



如果它不是日期类型,并且您正在提取日期或日期时间部分,则需要在将其转换为varchar(19)后将其转换为日期类型
You can only use certain operations on certain data types. You cannot datediff varchars.

If your input type is a date type then don't convert it

if it's not a date type, and you're extracting a date or datetime part then you need to convert that into a date type after you have converted it to varchar(19)


set格式



CONVERT(varchar(19),inputDate,106)

有一些格式可以将字符串转换为日期



找到sql格式的以下链接



[http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date -format.aspx [ ^ ]


这篇关于SQL Server错误从字符串转换日期和/或时间时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆