Problem using date comparison in Dynamic SQL Please help

  • Ok I am writing an archive process in SQL 2000 for an application. Long story short I need to grab values that are equal to or less than a date. The script does not work as created

    SAMPLE OUTPUT

    SELECT 0, po_number FROM AAD..t_receipt WHERE receipt_date <= 1/1/2013

    But if I put quotes around the date date at the end I do get the expected results

    SAMPLE OF MODIFIED SCRIPT

    SELECT 0, po_number FROM AAD..t_receipt WHERE receipt_date <= '1/1/2013'

    Below is the code that I am playing with. I need to get quotes around the date set. I have attempted that below but get an error.

    ERROR WHEN ATTEMPTING TO ENCLOSE DATE IN SINGLE QUOTE:

    Msg 403, Level 16, State 1, Procedure GenerateArchiveScipts, Line 242

    Invalid operator for data type. Operator equals divide, type equals varchar.

    Can someone please help? This is the last leg of this project and for the life of me I can't get past this final step. God I hate dealing with dates in SQL 2000.

    SET @SQLsource = 'SELECT '

    + CAST(@ArchiveBatchID AS varchar) + ', '+ @ArchiveControlField + ' FROM ' + 'AAD..' + @TableName + ' WHERE ' + @ArchiveSourceDateField + ' <= '''' + cast(datepart(mm, @ArchiveCutOffDate) as varchar) + '/' + cast(datepart(dd, @ArchiveCutOffDate) as varchar) + '/' + cast(datepart(yyyy, @ArchiveCutOffDate) as varchar) + '''

    <hr noshade size=1 width=250 color=#BBC8E5> Regards,Jeffery Williams http://www.linkedin.com/in/jwilliamsoh

  • You have some serious ' mismatches in your where clause.

    Try this:

    ' WHERE ' + @ArchiveSourceDateField + ' <= ''' + cast(datepart(mm, @ArchiveCutOffDate) as varchar) + '/' + cast(datepart(dd, @ArchiveCutOffDate) as varchar) + '/' + cast(datepart(yyyy, @ArchiveCutOffDate) as varchar) + ''''

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • DECLARE

    @SQLsource VARCHAR(1000),

    @ArchiveBatchID INT,

    @ArchiveControlField VARCHAR(20),

    @TableName VARCHAR(20),

    @ArchiveSourceDateField VARCHAR(20),

    @ArchiveCutOffDate DATETIME

    SELECT

    @ArchiveBatchID = 97,

    @ArchiveControlField = 'ControlColumn',

    @TableName = 'MyTable',

    @ArchiveSourceDateField = 'SourceDate',

    @ArchiveCutOffDate = GETDATE()

    SET @SQLsource = 'SELECT '

    + CAST(@ArchiveBatchID AS varchar) + ', '

    + @ArchiveControlField

    + ' FROM ' + 'AAD..' + @TableName

    + ' WHERE ' + @ArchiveSourceDateField

    + ' <= '''

    + CONVERT(VARCHAR(12),@ArchiveCutOffDate,112) + ''''

    SELECT @SQLsource

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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