tsql - Datetime conversion / mixed variables in dynamic SQL -


itemprop = "text">

I have a table that has more than one timeout column - TblDate1 , TblDate2 , TblDate3 , etc ...

I have a question that accepts a parameter to specify which date column to compare with another date Required.

Here is my query effort -

 sp_executesql  announcement @DateColumn nvarchar (128) set @DateColumn = 'TblDate2' declared @DateStart date set @DateStart = DATEADD (d, -7, getdate ()) Execute messages from N'SELECT * where @DueartinType & lt; @DateColumnIN ', N' @ DateStartIN datetime, @DateColumnIN nvarchar (128) ', @DateStartIN = @DateStart, @DateColumnIN = @DateColumn   

The return error -

  Conversions in message 241, level 16, state 1, line 1 fails when the character changes the date and / or time from the string.    

Try it out:

  DECLARE @DateColumn VARCHAR (128) SET @DateColumn = 'TblDate2' DECLARE @DateStart DATETIME SET @DateStart = DATEADD (D, -7, GETDATE ()) DECLARE @pvsSQL NVARCHAR (256) SET @pvsSQL = 'SELECT * with messageable WHERE' @DateColumn + '& Lt; '' '+ Convert (VARCHAR, @DateStart) +' ''; Executed 'sp_executesql @pvsSQL    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

jsp - Google line chart x-axis shrinks on transition -

java - Reaching JTextField in a DocumentListener -