DATEADD(Day,tblMyEventTableName.ReminderDays,@ DateNow)之间 CONVERT(smalldatetime,str(DATEPART(月,@ DateNow)+1)+''/''+ str(DATEPART(Day, tblMyEventTableName.TaskDateTime))+''/''+ str(DATEPART(年,@ DateNow)),101)和 tblMyEventTableName.RecurrenceEnd) I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + ''/'' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + ''/'' + str(DATEPART(Year, @DateNow)),101) AND tblMyEventTableName.RecurrenceEnd) 你的整个过程有点太复杂了,不能鼓励我深入细节,至少不是没有表格定义和样本数据。 但是如果我理解了他正确地说,如果 taskdatetime是例如20030131,它将无效,因为你将登陆不存在的日期20030231.看来你需要优化你的业务规则到盖住这个案子。 另外,我不知道你的桌子有多大,但是如果blMyEventTableName.ReminderDays上有一个索引,上面的表达式将不会使用该索引,因为该列是表达式的一部分。 Your entire procedure is a bit too complicated to encourage me to dive into the details, at least not without the table definition and sample data. But if I understand the above correctly, it will not work if taskdatetime is for instance 20030131, as you will land on the non-existing date 20030231. It seems that you need to refine your business rules to cover this case. Also, I don''t know how big your table is, but if there is a index on blMyEventTableName.ReminderDays, the expression above will not use that index, since the column is part of an expression. 我认为以下条款不正确: CONVERT(smalldatetime, str(DATEPART(月,@ DateNow)+1) 基本上你试图转换一个整数,即 月+ 1到一个smalldatetime 。我本来期望的是: CONVERT(int,str(DATEPART(月,@ DateNow)+1) - 例如 声明@DateNow smalldatetime 选择@DateNow =''31 -Dec-2003'' - 这会生成错误语法错误将字符串 转换为smalldatetime数据类型。 选择CONVERT(smalldatetime,str(DATEPART(月,@ DateNow)+1)) - 这个工作 选择CONVERT(int,str(DATEPART(月,@ DateNow)+1)) I think the following clause is not correct: CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) Basically you are attempting to convert an integer number, ie themonth + 1 to a smalldatetime. I would have expected something like: CONVERT(int,str(DATEPART(Month, @DateNow)+1)-- For exampledeclare @DateNow smalldatetimeselect @DateNow = ''31-Dec-2003'' -- This generates the error Syntax error converting character stringto smalldatetime data type.select CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1))-- This worksselect CONVERT(int,str(DATEPART(Month, @DateNow)+1)) 神秘人(Pr * ***********@hotmail.com)写道:Mystery Man (Pr************@hotmail.com) writes:我认为以下条款不正确: CONVERT(smalldatetime,str(DATEPART) (月,@ DateNow)+1) I think the following clause is not correct: CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) 是的,脱离背景这是非常不正确的,因为有一个 右边的假设这是完整的表达,重新格式化 易读性: CONVERT(smalldatetime, str(DATEPART(月,@ DateNow)+ 1)+''/''+ str(DATEPART(Day,tblMyEventTableName.TaskDateTime))+''/''+ str(DATEPART(年,@ DateNow)), 101) 我和你做了同样的反思,但我试过QA看看它实际上会回报什么?b $ b。需要一段时间来掌握语法错误... Anway,甚至完整的表达都不好,因为它没有 工作日期之类的2004-01-30。 - Erland Sommarskog,SQL Server MVP, so **** @ algonet.se SQL Server SP3联机丛书 http://www.microsoft.com/sql/techinf...2000/books.asp 这篇关于使用转换日期的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 12:09