Solved

Syntax error converting datetime from character string.

Posted on 2001-08-24
28
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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