Solved

dynamic sql not making the right where clause statement

Posted on 2007-11-30
5
179 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to remove numeric and alpha from an alphanumeric field? 5 85
Need a SQL Server 2014 plug-in to scan the DB schema 4 43
Foxpro errors 4 33
SQL Error - Query 6 26
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

809 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