Solved

Syntax error converting datetime from character string.

Posted on 2001-08-24
28
509 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
ID: 6422597
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
ID: 6422606
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6422619
try setting the dateformat also

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

Author Comment

by:raizon
ID: 6422628
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
ID: 6422633
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
ID: 6422657
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
ID: 6422668
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
ID: 6422671
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
ID: 6422680
by the way, are you at the mars co?
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6422692
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
ID: 6422697
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
ID: 6422702
helps if I give the right syntax: SUBSTRING(expression, start, length)
0
 
LVL 5

Author Comment

by:raizon
ID: 6422706
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
ID: 6422707
new line to try
    WHEN 'd' THEN 'DATEDIFF(day,[Date:],"' + @strDate + ') = 0'

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:raizon
ID: 6422713
>> 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
ID: 6422720
same error as before when using the new line John.
0
 
LVL 7

Accepted Solution

by:
John844 earned 100 total points
ID: 6422721
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
ID: 6422731
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
ID: 6422742
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
ID: 6422743

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
ID: 6422775
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
ID: 6422839
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
ID: 6422874
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
ID: 6422983
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
ID: 6423052
I think you need to add a space between 'SELECT....WHERE' + @strWhereClause to make them '....WHERE ' + @strWhereClause.
0
 
LVL 5

Author Comment

by:raizon
ID: 6423081
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
ID: 6423086
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
ID: 6423092
thanks, Glad to help
John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now