Returning records between 2 dates

  • and whats with the '' + ,

    Is this part of a dynamic sql select?

    executing this

    SELECT *

    FROM @a

    WHERE CTimeStamp BETWEEN '' + @DateFrom + '' AND '' + @DateTo + '''

    gives me Server: Msg 105, Level 15, State 1, Line 16

    Unclosed quotation mark before the character string ''

    '.

    Server: Msg 170, Level 15, State 1, Line 16

    Line 16: Incorrect syntax near ''

    '.

  • I have went with the following SQL code from the first reply by Michael Du Bois:

    CREATE PROCEDURE spGetDocuments

    @DateFrom datetime,

    @DateTo datetime,

    AS

    Declare @AddedDateTo datetime

    Set @AddedDateTo= DateAdd(DD,+1,@DateTo)

    SELECT RecID, Desc

    FROM tblFiles

    WHERE CTimeStamp BETWEEN ' + CHAR (39) + CONVERT (VARCHAR, @DateFrom, 101) + CHAR (39) + ' AND ' + CHAR (39) + CONVERT (VARCHAR, @AddedDateTo, 101) + CHAR (39)

    GO

  • That's why it fails... just use the variables directly >>

    where CTimeStamp >= @DateFrom and CTimeStamp < @AddedDateTo

  • Thanks Remi.  That's what I was trying to get at. 

    This was VERY good to learn about BETWEEN.  Thanks all. 

    I wasn't born stupid - I had to study.

  • BTW, the version I did is NOT the equivalent of BETWEEN (note the < instead of <=).

  • I saw that, but I figured Bendan had all the information needed... Just kind'a talkin'..., I was....

    I wasn't born stupid - I had to study.

  • Cool, we have a replacement for Sushila's comments .

  • Nah!  Her's are actually useful.   

    I wasn't born stupid - I had to study.

  • You got that right .

Viewing 9 posts - 16 through 23 (of 23 total)

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