日期:2014-05-17  浏览次数:20483 次

从字符串向 datetime 转换时失败。怎么解决
SQL code

declare @MonthsWage datetime
set @MonthsWage='2012-07-01'
declare @JiangXiangJinE  nvarchar(max)
select @JiangXiangJinE=coalesce(@JiangXiangJinE + ',' , 'update wage set ')  + en+'=b.'+en  from SystemDict where TableName='Awards'
set @JiangXiangJinE=@JiangXiangJinE+' from AwardsAmountSetting b where wage.PostID=b.PostID and MonthWage='''+@MonthsWage+''''
print @JiangXiangJinE

--提示
Msg 241, Level 16, State 1, Line 2
从字符串向 datetime 转换时失败。





------解决方案--------------------
declare @MonthsWage datetime
set @MonthsWage='2012-07-01'
declare @JiangXiangJinE nvarchar(max)
select @JiangXiangJinE=coalesce(@JiangXiangJinE + ',' , 'update wage set ') + en+'=b.'+en
from SystemDict where TableName='Awards'
set @JiangXiangJinE=@JiangXiangJinE+' from AwardsAmountSetting b where wage.PostID=b.PostID and MonthWage='''
+convert(varchar(10),@MonthsWage,120)+''''
print @JiangXiangJinE

------解决方案--------------------
你把 @MonthsWage 定义为字符串格式就可以了。
SQL code

declare @MonthsWage datetime
set @MonthsWage='2012-07-01'
print @MonthsWage 
--这样输出的结果是:07  1 2012 12:00AM

------解决方案--------------------
declare @MonthsWage datetime
set @MonthsWage='2012-07-01'
declare @JiangXiangJinE nvarchar(max)
select @JiangXiangJinE=coalesce(@JiangXiangJinE + ',' , 'update wage set ') + en+'=b.'+en from SystemDict where TableName='Awards'
set @JiangXiangJinE=@JiangXiangJinE+' from AwardsAmountSetting b where wage.PostID=b.PostID and 
MonthWage='''convert(varchar(10),@MonthsWage,120)'''' --这里修改不行? 
print @JiangXiangJinE
-->那你这个MonthWage字段是什么类型,字段值是什么样的
-->确认是否是这句出现错误
-->如果不能确认,给出表结构和错误附近的代码
------解决方案--------------------
declare @MonthsWage datetime
set @MonthsWage='2012-07-01'
declare @JiangXiangJinE nvarchar(max)
select @JiangXiangJinE=coalesce(@JiangXiangJinE + ',' , 'update wage set ') + en+'=b.'+en from SystemDict where TableName='Awards'
set @JiangXiangJinE=@JiangXiangJinE+' from AwardsAmountSetting b where wage.PostID=b.PostID and convert(varchar(10),MonthWage,21)='''+cast(@MonthsWage as varchar)+''''
print @JiangXiangJinE