using datetime variable in dynamic sql

  • Hi,

    I am trying to use datetime variable inside dynamic sql. it is giving me this error

    Syntax error converting datetime from character string.

    here is the sample code that I am running...

    DECLARE @pStart DATETIME

    SET @pStart = '10/27/2005'

    DECLARE @queryString VARCHAR(8000)

    SET @queryString = 'SELECT ' + @pStart

    SELECT @queryString

    Please let me know where I am doing wrong?

    Thanks,

    Sridhar.

  • in dynamic SQL you are building a string so you need to treat it that way as you would in QA:

    What you're trying to create is   SELECT '10/27/2005'

    So

    SET @query_string = 'SELECT ''' + CONVERT( VARCHAR(10), @pstart ) + ''''

     

  • Thanks John.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply