Syntax error converting datetime from character string.

If I execute the stored procedure in Query Analyzer like this.

sp_mars_search 't', 1, '5747','','','',10

I get the following error

"Syntax error converting datetime from character string."

If I comment out the two lines in the CASE that indicate @strDate the sp executes perfectly.

I've tried CAST and CONVERT both already.
I've also tried passing a date through.
I'm at a lost.
I'm also new to SP.  


Here is my stored procedure.

CREATE PROCEDURE sp_mars_search
(
  @strWhere VarChar(800),
  @intRecord Int,
  @strTracking VarChar(80),
  @strName VarChar(80),
  @strDate Datetime,
  @strLocation VarChar(80),
  @intRecordsPerPage Int
)
 AS

SET NOCOUNT ON

IF @strDate IS NULL SELECT @strDate = GETDATE()


DECLARE @strWhereClause varchar(255)
SELECT @strWhereClause =
     CASE @strWhere
     WHEN 't' THEN '[Tracking No:] like "%' + @strTracking + '%"'
     WHEN 'n' THEN '[To:] Like "%' + @strName + '%"'
     WHEN 'd' THEN '[Date:] = "' + @strDate + '"'
     WHEN 'l' THEN '[Location:] = "' + @strLocation + '"'
     WHEN 'tn' THEN '[Tracking No:] like "%' + @strTracking + '%" and [To:] Like "%' + @strName + '%"'
     WHEN 'td' THEN '[Tracking No:] like "%' + @strTracking + '%" and [Date:] = "' + @strDate + '"'
     WHEN 'tl' THEN '[Tracking No:] like "%' + @strTracking + '%" and [Location:] = "' + @strLocation + '"'
     END

CREATE TABLE #TempMars
(
     tMarsID Int IDENTITY,
     marsID Int,
     [To:] VarChar(50),
     [Tracking No:] VarChar(255),
     [Date:] DateTime,
     [Location:] varchar(50),
     [Phone No:] varchar(50),
     [From:] varchar(50),
     [Rec Location:] varchar(50),
     [Rec Phone No:] varchar(50)
)
     

DECLARE @SearchSQL varchar(255)
SELECT @SearchSQL = 'INSERT INTO #TempMars SELECT * FROM marsPTS WHERE'  + @strWhereClause
EXECUTE(@SearchSQL)

DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@intRecord -1) * @intRecordsPerPage
SELECT @LastRec = (@intRecord * @intRecordsPerPage + 1)

DECLARE @CountSQL varchar(255)
SELECT @CountSQL = 'SELECT MoreRecords = COUNT(*)FROM #TempMars WHERE' + @strWhereClause
EXECUTE(@CountSQL)

SELECT * FROM #TempMars WHERE tMarsID > @FirstRec AND tMarsID < @LastRec


SET NOCOUNT OFF


I'm offereing all the points I have left.  After the first of the month I'll have more (I qualified for KPro :-))
LVL 5
raizonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John844Commented:
change this line from
    WHEN 'd' THEN '[Date:] = "' + @strDate + '"'
to
    WHEN 'd' THEN '[Date:] = "' + cast(@strDate as varchar) + '"'
0
AlfaNoMoreCommented:
try setting the dateformat also

SET DATEFORMAT dmy -- UK setting
SET DATEFORMAT mdy -- Good for funny foreigners :-)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

raizonAuthor Commented:
Thanks John  that works beautifully except for when I search on the date 'd' or tracking# date 'td'

The two following return an empty recordset

sp_mars_search 'td', 1, '5747','','8/14/2001','',10
--should return 2 records

sp_mars_search 'd', 1, '','','8/14/2001','',10
--should return 10 records with a total of 315 records.
0
raizonAuthor Commented:
thanks for the article thunderchicken.  I checked into that one.  We are running SQL7 with SP3.  I'm not doing any UNIONS on the tables either so I wasn't sure if that applied.
0
John844Commented:
it probably has to do with how the database is storing the dates.  I bet the dates have data like 1/1/2001 00:00:00 or somthing very similar.
0
raizonAuthor Commented:
yes they do.  What can be done.  There are over 20,000 rows of in the table so changing that isn't really going to be an option.  I'm trying to increase the performance of a working application by taking all the query's and converting them to SP.
0
John844Commented:
sorry, hit submit before I was done typing.

You are then trying to compare "1/1/2001" to "1/1/2001 00:00:00" which is not equal.  I have had to try tricks in the past to get around this.  I am not sure if there is a better way, but this is how I did it.
I had to check if the date was greater than the beginning of the day and before the beginning of the next day.
date >= "1/1/2001" and date < "1/2/2001"
0
John844Commented:
by the way, are you at the mars co?
0
AlfaNoMoreCommented:
I think something like substring([Date:],10) = @date

might do the trick. This'll remove all timepart from the date.

Of course, if @date was a datetime variable, you wouldn't need to. But this might be the easiest way of doing it?

Daniel.
0
John844Commented:
this is the function I would recommend adding to code
SELECT DATEDIFF(day, date1, date2)
code to follow, just give me a couple of minutes to type it in.

0
AlfaNoMoreCommented:
helps if I give the right syntax: SUBSTRING(expression, start, length)
0
raizonAuthor Commented:
here is something that might help.

I added this to the sp

SELECT var1 = @strWhereClause

so that I could see the exact WHERE clause it was using

this is what its using when doing a search on a date.


[Tracking No:] like "%5747%" and [Date:] = "Aug 14 2001 12:00AM"
0
John844Commented:
new line to try
    WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + ') = 0'

0
raizonAuthor Commented:
>> by the way, are you at the mars co?

no I wish I was.

mars = Materials Action Request.

PTS = Package Tracking System.
0
raizonAuthor Commented:
same error as before when using the new line John.
0
John844Commented:
oops, left off a quote, new line to try
   WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + '") = 0'

ps, just working on the first date for now, same code will apply to the tracking and date later.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlfaNoMoreCommented:
Also consider DATEPART if the SUBSTRING doesn't work.

DATEPART("dd", [Date:])
DATEPART("mm", [Date:])
DATEPART("yy", [Date:])

And then just concatinate them all together

WHERE [Date:] = (DATEPART("dd", [Date:]) + '/' + DATEPART("mm", [Date:]) + '/' + DATEPART("yy", [Date:]))

Messy, but should do the job.
0
AlfaNoMoreCommented:
That's sooooo wrong. But sure youc an figure out what Iwas trying to do :-(

Feel ashamed now, I'm going home!!
0
John844Commented:

here is a simple example that I used on my DB and got it to return the proper recordset.
select * from xmlcalcoutput where DATEDIFF(day,LastUpdated,"8/7/01") = 0

this only returned records with dates on 8/7/01 no matter what time was added to the dates in the DB.
0
raizonAuthor Commented:
lol Alfa,

Thanks for the help.  It gives me stuff to work on :-)

John,

we are getting closer.  I've reset @strDate to varchar(80) in the declarations at the top.  Now when using the DATEDIFF I get the following error.

Server: Msg 155, Level 15, State 1, Line 1
'day' is not a recognized OPTIMIZER LOCK HINTS option.
0
John844Commented:
sorry, my mind is a little blurry right now.  can you post the current version of the sp.
0
John844Commented:
if @strDate is now a varchar then the following would be my next attempt.

SELECT @strWhereClause =
    CASE @strWhere
    WHEN 't' THEN '[Tracking No:] like "%' + @strTracking + '%"'
    WHEN 'n' THEN '[To:] Like "%' + @strName + '%"'
    WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + '") = 0'
    WHEN 'l' THEN '[Location:] = "' + @strLocation + '"'
    WHEN 'tn' THEN '[Tracking No:] like "%' + @strTracking + '%" and [To:] Like "%' + @strName + '%"'
    WHEN 'td' THEN '[Tracking No:] like "%' + @strTracking + '%" and DATEDIFF(day,[Date:],"' + @strDate + '") = 0'
    WHEN 'tl' THEN '[Tracking No:] like "%' + @strTracking + '%" and [Location:] = "' + @strLocation
+ '"'
    END
0
John844Commented:
you might have to increase the size of your strings to store the sql statements.  If the value gets too big, it is just truncated and doesn't warn you.  I would increase them to
DECLARE @SearchSQL varchar(500)
and
DECLARE @CountSQL varchar(500)

print out the values right before you execute them to make sure they look as expected.

0
John844Commented:
I think you need to add a space between 'SELECT....WHERE' + @strWhereClause to make them '....WHERE ' + @strWhereClause.
0
raizonAuthor Commented:
the space got rid of there error.  The SP is now working.  Thank you so much once again John.
0
raizonAuthor Commented:
Here is the final SP.

ALTER PROCEDURE sp_mars_search
(
  @strWhere VarChar(800),
  @intRecord Int,
  @strTracking VarChar(80),
  @strName VarChar(80),
  @strDate VarChar(80),
  @strLocation VarChar(80),
  @intRecordsPerPage Int
)
 AS

SET NOCOUNT ON

DECLARE @strWhereClause varchar(255)
SELECT @strWhereClause =
     CASE @strWhere
     WHEN 't' THEN '[Tracking No:] like "%' + @strTracking + '%"'
     WHEN 'n' THEN '[To:] Like "%' + @strName + '%"'
     WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + '") = 0'
     WHEN 'l' THEN '[Location:] = "' + @strLocation + '"'
     WHEN 'tn' THEN '[Tracking No:] like "%' + @strTracking + '%" and [To:] Like "%' + @strName + '%"'
     WHEN 'td' THEN '[Tracking No:] like "%' + @strTracking + '%" and DATEDIFF(day,[Date:],"' + @strDate + '") = 0'
     WHEN 'tl' THEN '[Tracking No:] like "%' + @strTracking + '%" and [Location:] = "' + @strLocation + '"'
     END

CREATE TABLE #TempMars
(
     tMarsID Int IDENTITY,
     marsID Int,
     [To:] VarChar(50),
     [Tracking No:] VarChar(255),
     [Date:] DateTime,
     [Location:] varchar(50),
     [Phone No:] varchar(50),
     [From:] varchar(50),
     [Rec Location:] varchar(50),
     [Rec Phone No:] varchar(50)
)
     

DECLARE @SearchSQL varchar(500)
SELECT @SearchSQL = 'INSERT INTO #TempMars SELECT * FROM marsPTS WHERE '  + @strWhereClause
EXECUTE(@SearchSQL)

DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@intRecord -1) * @intRecordsPerPage
SELECT @LastRec = (@intRecord * @intRecordsPerPage + 1)

DECLARE @CountSQL varchar(500)
SELECT @CountSQL = 'SELECT MoreRecords = COUNT(*)FROM #TempMars WHERE ' + @strWhereClause
EXECUTE(@CountSQL)

SELECT * FROM #TempMars WHERE tMarsID > @FirstRec AND tMarsID < @LastRec


SET NOCOUNT OFF

John you have 100 more pts coming when I get them.  Thanks again
0
John844Commented:
thanks, Glad to help
John
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.