Solved

dynamic sql not making the right where clause statement

Posted on 2007-11-30
5
174 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
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 125 total points
Comment Utility
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
Comment Utility
I assume @StatusDateEnd and @StatusDateEnd are datetimes
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
I am still a little confused about the escaping the '.  It doesn't quite seem consistient to me
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now