日期:2014-05-18  浏览次数:20528 次

从字符串向 datetime 转换时失败
declare @Umon datetime
SET @Umon=convert(datetime,(SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))),120)

declare @temsql nvarchar(max)
set @temsql='SELECT * FROM T1 Where Sort=1 And (MAK_DAT>='''
set @temsql=@temsql+@Umon
set @temsql=@temsql+''') And (MD <1000)'

print @temsql


提示如下:
消息 241,级别 16,状态 1,第 6 行
从字符串向 datetime 转换时失败。

------解决方案--------------------
SQL code

DECLARE @Umon DATETIME
SET @Umon = CONVERT(DATETIME, ( SELECT  DATEADD(ms, -3,
                                                DATEADD(mm,
                                                        DATEDIFF(mm, 0,
                                                              GETDATE()), 0))
                              ), 120)

DECLARE @temsql NVARCHAR(MAX)
SET @temsql = 'SELECT * FROM T1 Where Sort=1 And (MAK_DAT>='''
SET @temsql = @temsql + CONVERT(VARCHAR(20),@Umon,120)
SET @temsql = @temsql + ''') And (MD <1000)'

PRINT @temsql