Two identical Queries are returning different results in where clause. The first query is with SQL parameter variable and the second query with a string value as mentioned below. The result of both queries should be the same as both have the same values but it is not happening. what could be the reason for that?
Declare @messageID AS INT=720987
Declare @replyfileType AS Varchar='Audio'
select VoiceMessageReply from MDBDetails where MessageID = @messageID and FileType=@replyfileType
select VoiceMessageReply from MDBDetails where MessageID = @messageID and FileType='Audio'
Result is as :- Query results
This declaration:
Declare @replyfileType AS Varchar = 'Audio';
Is declaring a one character string, so it is equivalent to:
Declare @replyfileType AS Varchar(1) = 'A';
Always include the length when using strings in SQL Server!
Declare @replyfileType AS Varchar(255) = 'Audio';
I should note that if you printed out @replyfileType
the error would be obvious.