Michael
Michael

Reputation: 1607

SQL string comparison: not equal but SQL says equal

I have a simple stored procedure named sp_isSessionActive that takes a parameter named 'token' containing the following SQL:

SELECT 1 AS Active 
FROM dbo.Mobile_Session
WHERE Token = @token AND GETDATE() BETWEEN StartTime AND EndTime;

If I invoke that procedure with a token that exists in the database and is in the correct timespan, I get the correct result, 1.

EXEC sp_issessionactive @token='E883735B40CF4F939EAD133992770C87'

But if I add anything at the end of the token(which of course doesn't exist in the table), I also get 1!

exec sp_issessionactive @token='E883735B40CF4F939EAD133992770C87aaaaaaaaaaaaaaaaa'

What's going on? There's something I'm missing in the way SQL Server handles string comparisons..

Upvotes: 0

Views: 284

Answers (1)

marc_s
marc_s

Reputation: 754508

Show us the stored procedure code!

Most importantly - how are the parameters of the stored procedure defined?

What's the datatype for your parameter @Token?

Is it just VARCHAR by any chance?

In that case - you've defined a parameter of 1 character length - so of course it cannot distinguish those two strings you pass .....

Upvotes: 4

Related Questions