从字符串转换datetime时转换失败

时间:2010-12-16 14:04:21

标签: sql-server-2005

喜 我在从createddate和leavestartdate列上的字符串转换datetime时遇到问题.....

由varchar(30)创建的表结构,createddate datetime,leavetype varchar(30),leavestartdate varchar(30),status varchar(30)

ALTER Procedure [dbo].[sp_SearchLeave]
@createdby      varchar(30) = null ,
@createddate    DateTime    = null ,
@leavetype      varchar(30) = null ,
@leavestartdate varchar(30) = null ,
@status         varchar(30) = null

As
Begin

if @createddate is not null and Len(@createddate) = 0 set @createddate = null
if @leavetype = 'Select' set @leavetype = null
if @leavestartdate is not null and Len(@leavestartdate) = 0 set @leavestartdate = null
if @status = 'Select' set @status = null

Select leaverequestid,leaveenddate,leavetype,leavestartdate,status
from LeaveRequest
where 
       (@createdby is null or createdby like '%' + @createdby + '%')
  and  (@createddate is null or createddate like '%' + @createddate + '%')
  and  (@leavetype is null or leavetype like '%' + @leavetype + '%')
  and  (@leavestartdate is null or leavestartdate like '%' + @leavestartdate + '%')
  and  (@status is null or status like '%' + @status + '%')
End

当我执行此sp作为输入时 exec sp_SearchLeave'','','','12/15/2010',''

它显示错误信息,如
消息241,级别16,状态1,过程sp_SearchLeave,第26行 从字符串转换datetime时转换失败。

1 个答案:

答案 0 :(得分:3)

原因可能是like声明。您将datetime参数(@createddate)视为字符串,方法是将%附加到字符串,这是您无法做到的。