Link to home
Start Free TrialLog in
Avatar of raizon
raizon

asked on

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 :-))
Avatar of John844
John844

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

SET DATEFORMAT dmy -- UK setting
SET DATEFORMAT mdy -- Good for funny foreigners :-)
Avatar of raizon

ASKER

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.
Avatar of raizon

ASKER

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.
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.
Avatar of raizon

ASKER

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.
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"
by the way, are you at the mars co?
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.
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.

helps if I give the right syntax: SUBSTRING(expression, start, length)
Avatar of raizon

ASKER

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

Avatar of raizon

ASKER

>> by the way, are you at the mars co?

no I wish I was.

mars = Materials Action Request.

PTS = Package Tracking System.
Avatar of raizon

ASKER

same error as before when using the new line John.
ASKER CERTIFIED SOLUTION
Avatar of John844
John844

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
That's sooooo wrong. But sure youc an figure out what Iwas trying to do :-(

Feel ashamed now, I'm going home!!

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.
Avatar of raizon

ASKER

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.
sorry, my mind is a little blurry right now.  can you post the current version of the sp.
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
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.

I think you need to add a space between 'SELECT....WHERE' + @strWhereClause to make them '....WHERE ' + @strWhereClause.
Avatar of raizon

ASKER

the space got rid of there error.  The SP is now working.  Thank you so much once again John.
Avatar of raizon

ASKER

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
thanks, Glad to help
John