Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Problem using date comparison in Dynamic SQL Please help Expand / Collapse
Author
Message
Posted Monday, July 8, 2013 7:55 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:10 AM
Points: 143, Visits: 626
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
Post #1471199
Posted Monday, July 8, 2013 8:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 12,995, Visits: 12,414
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1471209
Posted Monday, July 8, 2013 8:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:37 AM
Points: 6,748, Visits: 13,890
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1471217
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse