Solved

dynamic sql not making the right where clause statement

Posted on 2007-11-30
5
189 Views
Last Modified: 2010-03-19
Hi,
I'm still working on converting the query to dynamic SQL.  The DB is SQL Server 2005.  In this particular part of the query there are two parameters passed in for the begining and end dates.  Below is the statement that worked.  This part is being converted simply because  some other parts of the code require it.

Original code that worked:

AND cast(CASE ISDATE(RTRIM(LTRIM(MAT1_01_07))) WHEN 0 THEN '1/1/1901' ELSE RTRIM(LTRIM(MAT1_01_07))

END as datetime) BETWEEN  @StatusDateBegin AND @StatusDateEnd

--------------------------
Below is the dynamic conversion code of above:

-------------------------Status Date Solution------------------
SET @StatusDates = ' AND cast(CASE ISDATE(RTRIM(LTRIM(StatusDate))) '  
      +  ' WHEN 0 THEN ' + '''1/1/1901'''         
      + ' ELSE RTRIM(LTRIM(StatusDate)) END as datetime) '
      + ' BETWEEN ' + @StatusDateBegin + ' AND ' + @StatusDateEnd
SET @SQL_WHERE = @SQL_WHERE + @StatusDates

--------------------
Below is the actual part of the where clause that is built. This is should be much more complete:

AND cast(CASE


Thanks,
0
Comment
Question by:FairSkies
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 125 total points
ID: 20386727
SET @StatusDates = ' AND cast(CASE ISDATE(RTRIM(LTRIM(StatusDate))) '  
      +  ' WHEN 0 THEN ' + '''1/1/1901'''        
      + ' ELSE RTRIM(LTRIM(StatusDate)) END as datetime) '
      + ' BETWEEN ''' + convert(varchar, @StatusDateBegin, 102) + ''' AND ''' + convert(varchar, @StatusDateEnd, 102) + ''''
SET @SQL_WHERE = @SQL_WHERE + @StatusDates
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20386728
I assume @StatusDateEnd and @StatusDateEnd are datetimes
0
 
LVL 32

Expert Comment

by:awking00
ID: 20388677
I also assume that StatusDate is also a datetime, so it won't look anything like the "MAT1_01_07" string that worked. I think you need to cast it as a character string before applying the trim functions to it (and may also require some further manipulation if it needs to look like MAT1_01_07).
0
 

Author Comment

by:FairSkies
ID: 20398014
Hi,
Thanks for the help.  StatusDate is datetime in the view and the input parameters @StatusDateEnd are @ServiceDateBegin are are both datetime.
0
 

Author Comment

by:FairSkies
ID: 20417376
I am still a little confused about the escaping the '.  It doesn't quite seem consistient to me
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
denied execute as 13 56
Error 438 6 48
Trying to understand why my Index is so large 12 51
What are the recommended security measures to put in place? 19 87
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question