数据类型varchar(max)和日期在add运算符

时间:2015-04-23 12:12:40

标签: sql-server sql-server-2008 sql-server-2008-r2

我在这里创建了一个搜索SP,只是根据屏幕上的用户输入准备参数。

现在,问题是,我需要使用DATE类型转换表列以及输入参数,然后需要查询。

以下是我需要实现的程序部分。

问题是,现在收到以下错误:The data types varchar(max) and date are incompatible in the add operator.

  DECLARE @WhereClause VARCHAR(MAX)
   DECLARE @DateField DATETIME = GETDATE()
   DECLARE @DateFieldTo DATETIME =  GETDATE() +1

  SET @WhereClause = @WhereClause + ' AND CAST(tbl.DateField AS DATE) BETWEEN ''' + CAST(@DateField AS DATE) + ''' + AND ''' +  CAST(@DateFieldTo AS DATE)+ ''        

  PRINT @WhereClause

请解决此问题。

尝试以下方法:

    Error:   Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

   DECLARE @WhereClause VARCHAR(MAX)
   DECLARE @DateField DATETIME = GETDATE()
   DECLARE @DateFieldTo DATETIME =  GETDATE() +1
   DECLARE @YourSQLVariable VARCHAR(MAX)


     SET @WhereClause = @WhereClause + ' AND CAST(tbl.DateField AS DATE) BETWEEN  CAST(@DateField AS DATE) + AND  CAST(@DateFieldTo AS DATE)'

   SET @YourSQLVariable = 'SELECT 1 WHERE' + @WhereClause

    EXEC sp_executeSQL @YourSQLVariable,N'@DateField   DATETIME,@DateFieldTo DATETIME ',@DateField,@DateFieldTo

由于

4 个答案:

答案 0 :(得分:3)

它,因为您尝试在此行中将DATE类型CAST(tbl.DateField AS DATE)与字符串@WhereClause连接起来,但不支持

  SET @WhereClause = @WhereClause + ' AND CAST(tbl.DateField AS DATE) BETWEEN ''' + CAST(@DateField AS DATE) + ''' + AND ''' +  CAST(@DateFieldTo AS DATE)+ ''   

你可以使用类似的东西来实现这个目标

  SET @WhereClause = @WhereClause + ' AND CAST(tbl.DateField AS DATE) BETWEEN  CAST(@DateField AS DATE) + AND  CAST(@DateFieldTo AS DATE)' 

并且在运行sp_executeSQL时,请使用此

@YourSQLVariable = @SelectClause + @WhereClause
EXEC sp_executeSQL @YourSQLVariable,N'@DateField DATETIME,@DateFieldTo DATETIME ',@DateField,@DateFieldTo

答案 1 :(得分:1)

您可以尝试使用日期转换或日期比较仅w.r.t date,

DECLARE @SqlQuery VARCHAR(MAX) = ''
DECLARE @UserId VARCHAR(MAX) = 'TestUser'
DECLARE @StartDate DATETIME = GETDATE()
DECLARE @EndDate DATETIME = DATEADD(WEEK, -6, GETDATE())
SET @SqlQuery = 'SELECT * FROM User Z WHERE Z.Id = ''' + @UserId + ''' AND CAST(Z.CreatedDate AS DATE) BETWEEN CAST(''' + CONVERT(NVARCHAR(24), @StartDate, 101) + ''' AS DATE) AND CAST(''' + CONVERT(NVARCHAR(24), @ENDDATE, 101)  +''' AS DATE)'

答案 2 :(得分:0)

您无法使用字符串连接日期对象:

DECLARE @WhereClause VARCHAR(MAX)
DECLARE @DateField DATETIME = GETDATE()
DECLARE @DateFieldTo DATETIME =  GETDATE() +1

SET @WhereClause = @WhereClause + ' AND CONVERT(VARCHAR(10),tbl.DateField,120) BETWEEN ''' + CONVERT(VARCHAR(10),@DateField,120) + ''' + AND ''' +  CONVERT(VARCHAR(10),@DateFieldTo,120)+ ''''        

PRINT @WhereClause

答案 3 :(得分:0)

SQL不允许字符串与其他数据类型连接。

DECLARE @WhereClause VARCHAR(MAX) = ''
DECLARE @DateField DATETIME = GETDATE()
DECLARE @DateFieldTo DATETIME =  GETDATE() +1

SET @WhereClause = @WhereClause + ' AND CAST(tbl.DateField AS DATE) BETWEEN '''  + CONVERT(VARCHAR(25), @DateField, 120) + ''' AND ''' +  CONVERT(VARCHAR(25), @DateFieldTo, 120) + ''''

PRINT @WhereClause

此外,未定义@WhereClause,因此连接不能使用初始空字符串。