Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem using date comparison in Dynamic SQL Please help


Problem using date comparison in Dynamic SQL Please help

Author
Message
Jeffery Williams
Jeffery Williams
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 913
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
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)
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19014
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search