Solved

Syntax error converting datetime from character string.

Posted on 2001-08-24
28
508 Views
Last Modified: 2012-05-04
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 :-))
0
Comment
Question by:raizon
  • 13
  • 9
  • 5
  • +1
28 Comments
 
LVL 7

Expert Comment

by:John844
Comment Utility
change this line from
    WHEN 'd' THEN '[Date:] = "' + @strDate + '"'
to
    WHEN 'd' THEN '[Date:] = "' + cast(@strDate as varchar) + '"'
0
 
LVL 11

Expert Comment

by:thunderchicken
Comment Utility
0
 
LVL 9

Expert Comment

by:AlfaNoMore
Comment Utility
try setting the dateformat also

SET DATEFORMAT dmy -- UK setting
SET DATEFORMAT mdy -- Good for funny foreigners :-)
0
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
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
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
by the way, are you at the mars co?
0
 
LVL 9

Expert Comment

by:AlfaNoMore
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
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
 
LVL 9

Expert Comment

by:AlfaNoMore
Comment Utility
helps if I give the right syntax: SUBSTRING(expression, start, length)
0
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
new line to try
    WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + ') = 0'

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Author Comment

by:raizon
Comment Utility
>> by the way, are you at the mars co?

no I wish I was.

mars = Materials Action Request.

PTS = Package Tracking System.
0
 
LVL 5

Author Comment

by:raizon
Comment Utility
same error as before when using the new line John.
0
 
LVL 7

Accepted Solution

by:
John844 earned 100 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:AlfaNoMore
Comment Utility
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
 
LVL 9

Expert Comment

by:AlfaNoMore
Comment Utility
That's sooooo wrong. But sure youc an figure out what Iwas trying to do :-(

Feel ashamed now, I'm going home!!
0
 
LVL 7

Expert Comment

by:John844
Comment Utility

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
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
sorry, my mind is a little blurry right now.  can you post the current version of the sp.
0
 
LVL 7

Expert Comment

by:John844
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
I think you need to add a space between 'SELECT....WHERE' + @strWhereClause to make them '....WHERE ' + @strWhereClause.
0
 
LVL 5

Author Comment

by:raizon
Comment Utility
the space got rid of there error.  The SP is now working.  Thank you so much once again John.
0
 
LVL 5

Author Comment

by:raizon
Comment Utility
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
 
LVL 7

Expert Comment

by:John844
Comment Utility
thanks, Glad to help
John
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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