declare @SQL_update nvarchar(2048) 
declare @sheetno nvarchar(10)
declare @calltimereal datetime
set @SQL_update = 'update #t set ' + substring(@SQL_update,2,len(@SQL_update)) + ' where [维修单号]=''' + @sheetno + '''  and [回访时间] = cast ('''+ @calltimereal+''' as datetime)'

exec sp_executesql @SQL_update 抱错
从字符串向 datetime 转换时失败。该如何转换呢?sql字符串

解决方案 »

  1.   

    set @SQL_update = 'update #t set ' + substring(@SQL_update,2,len(@SQL_update)) + ' where [维修单号]=''' + @sheetno + '''  and [回访时间] = cast ('''+ Convert(varchar(19),@calltimereal,120)+''' as datetime)'
      

  2.   

    看一下@calltimereal的真正值是什么。报错的结果显示这个值无法转换成时间类型。 
      

  3.   

    首先我想问下 这个SQL(@SQL_update)成立么? 咋看着不对啊.
      

  4.   

    set @SQL_update = 'update #t set ' + substring(@SQL_update,2,len(@SQL_update)) + ' where [维修单号]=''' + @sheetno + '''  and [回访时间] = cast ('''+ convert(varchar,@calltimereal,121)+''' as datetime)'执行时不在数据匹配时转换也行,数据库会自动隐式转换
    set @SQL_update = 'update #t set ' + substring(@SQL_update,2,len(@SQL_update)) + ' where [维修单号]=''' + @sheetno + '''  and [回访时间] = '''+ convert(varchar,@calltimereal,121)+''' '