1

I have this SQL Server query which is returning Conversion failed when converting date and/or time from character string.

(
    @numberRecords int,
    @date varchar(12)
)

AS
SET ROWCOUNT @numberRecords
SELECT re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges re
    FULL OUTER JOIN Revisions rev
        ON re.RevisionID = rev.ID
    FULL OUTER JOIN Logs lg
        ON re.LogID = lg.ID
WHERE convert(datetime, rev.Timestamp) >= '@date' OR
      convert(datetime, lg.Timestamp) >= '@date';
RETURN

The date parameter returns a string value in the format "dd-MM-yyyy". What may be the cause?

6
  • 1
    why not store rev.Timestamp and lg.Timestamp as datetime columns? Commented May 4, 2012 at 18:38
  • 1
    +1, @KM. - Nothing good ever comes from storing dates or numbers as strings Commented May 4, 2012 at 18:40
  • Yes, both timestamps are stored as datetime Commented May 4, 2012 at 18:41
  • @JNK, sorting problems, conversion problems, and invalid data! Commented May 4, 2012 at 18:41
  • 1
    @chrisportelli then why are you converting them? Commented May 4, 2012 at 18:41

3 Answers 3

4

Don't put your @date parameter inside single quotes - it's being interpreted as a string instead of as a parameter.

As written you are trying to make '@date' a datetime which won't parse.

That being said, you should probably make the input parameter a datetime in the first place.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, the code is comparing the table columns against the sting literal "@date". You need to compare the table columns against the variable @date. If you say MyColumn='abc' you are comparing your column against the constant string "abc". if you say MyColumn='@abc' you are still comparing you column against the a constant string "@abc". if you say MyColumn=@abc you are comparing your column against the value within the @abc variable.
1

remove the single quotes as @JNK has described to make your query work. However, for possibly much better performance (index usage) try this query (if you have indexes on the columns Revisions.Timestamp and Logs.Timestamp) :

SELECT 
    re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges         re
    INNER JOIN Revisions   rev ON re.RevisionID = rev.ID
WHERE rev.Timestamp >= @date
UNION
SELECT 
    re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges    re
    INNER JOIN Logs   lg ON re.LogID = lg.ID
WHERE lg.Timestamp >= @date

Comments

0

it looks to me, at first glance, that you are converting to datetime, not from it. I am assuming rev.Timestamp is a datetime already, while your @date is a varchar(12)...so SQL Server is complaining that it can't compare a datetime to a string...

2 Comments

I'm not sure what this answer means...You're basically talking in circles.
no, i'm not. your convert statement converts a datetime to another datetime, takes the result and compares it to a varchar(12)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.