简体   繁体   中英

Conversion failed when converting date and/or time from character string?

Any Reason why this keeps happening even though the query is valid?... My code is below :)

Select Cdf_Code_Tx, ProjectNo, Convert (Date, [Last Call Back],103) As LastCallBack, (CDF_New_des1_Tx+CDF_New_des2_Tx) As Description
From Products
Inner Join SpecDetails
On CDF_Code_Tx = ProductRef
Inner Join Projects2
On ProjectNo = PID
WHERE  [Last Call Back] = (SELECT MAX(Convert (Date, [Last Call Back], 103))FROM Projects2)
Order By Convert (Date, [Last Call Back], 103) 

When i run this , this error appears!

Msg 241, Level 16, State 1, Line 1 Conversion failed when converting date and/or time from character string.

Needing guidance & Thank you in advance.

我认为你的问题在这里因为我似乎[Last Call Back]是varchar()数据类型,当你转换varchar数据类型是日期并与varchar比较然后你有这个错误请你应该改变相同数据类型的两个条件

WHERE  [Last Call Back] = (SELECT MAX(Convert (Date, [Last Call Back], 103))FROM Projects2)

I think you need to check data for [Last Call Back] column in your table. Check that all data are valid for date conversion or not. I think some string are not valid to convert it into date. So you need to check it by ISDate() function. ISDDate() function will work based on your language and culture like below.

SET LANGUAGE british
SELECT ISDATE('2013-11-20')   /* will return 0 */

SET LANGUAGE us_english
SELECT ISDATE('2013-11-20')   /* will return 1 */

For example, If your column have format like below then it will give error

SELECT CONVERT(DATE, '2013-11-20', 103)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM