当前位置: 代码迷 >> Sql Server >> 从字符串向datetime变换时失败
  详细解决方案

从字符串向datetime变换时失败

热度:39   发布时间:2016-04-24 22:45:49.0
从字符串向datetime转换时失败
SQL 2005


 declare @StartDate datetime,
         @EndDate dateTime

select @tsql = @tsql + ' and a.UP_Time between ' + @StartDate + ' and ' + @EndDate


上述是时间比较的问题。编译没报错,执行就报错“从字符串向datetime转换时失败”
哪里写错了? 

------解决方案--------------------
'"++"'
------解决方案--------------------
 DECLARE @tsql NVARCHAR(max)
 SET @tsql=''
 declare @StartDate DATETIME='2013-04-26 00:00:01', 
         @EndDate dateTime ='2013-04-26 15:00:01'
  --PRINT CONVERT(VARCHAR(30),@StartDate,120)
  --PRINT CONVERT(VARCHAR(30),@EndDate,120)

SELECT   @tsql =@tsql+ ' and a.UP_Time between ' +''''+CONVERT(VARCHAR(30),@StartDate,120)+'''' +' and ' +''''+CONVERT(VARCHAR(30),@EndDate,120)+''''
print @tsql
  相关解决方案