Quantcast
Viewing all articles
Browse latest Browse all 13329

Strangest thing... Getting different results when checking for string in a query.

Hello

I cannot see nor understand why one query would return something different than the other...

Here's the queries:
Code:

DECLARE @searchString NVARCHAR(255)
SET @searchString = 'EHA'

SELECT t1.id, t1.name FROM users as t1
WHERE  t1.name LIKE  '%' + 'EHA' + '%')
   
SELECT 1.id, t1.name FROM users as t1
WHERE t1.name LIKE  '%' + @searchString + '%')

The first query (select-statement), where I've manually written 'EHA' returns 6 rows, while the second query returns 5 rows, it is missing on the name "EHAA". The "Name" field is of type varchar(4), not null.
Image of the results:
http://s8.postimage.org/a2v9yup8l/Capture.png

Suggestions?

Holy... :)

The field is of type varchar... I've declared a nvarchar-variable, so problem is "solved", but still not understand why the nvarchar did not work. I've also tried with adding a TRIM to the search string as follows:
SET @searchString = RTRIM(@searchString);
SET @searchString = LTRIM(@searchString);

But it resulted in the same... Still do not understand why it would fail on "EHAA" and not the others...Probably because it began with "EHA", and it skipped the first character in the string, somehow...

Viewing all articles
Browse latest Browse all 13329

Trending Articles