By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,266 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,266 IT Pros & Developers. It's quick & easy.

LIKE Condition while using parameters

P: 4
Hi All,

I'm trying to create a search stored procedure within MSSQL.

I've come up with the following:
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[usp_Search]
  2. @SearchText nchar(15)
  3. AS
  4. BEGIN
  5. SELECT *
  6. FROM         dbo.Person
  7. WHERE FirstName LIKE '%'+@SearchText+'%'
  8. END
I've tested this a few times on the test data I have in the database but it does not seem to be working correctly, although when I manually put something in, ie - take the parameter out and type in for example: WHERE FirstName LIKE '%at%' it will work...

Can anyone explain to me what I am doing incorrectly?
Mar 6 '09 #1
Share this Question
Share on Google+
4 Replies


P: 4
I just realised this is in the MySQL threads...oops.

It's MS SQL.
Mar 6 '09 #2

P: 9
Hey Matt,

Try changing @SearchText to nvarchar(15) instead of nchar(15). Since it is char, it is padding the value with spaces so that it is 15 characters in total length. Therefore, when you are searching for 'at' you are really searching for '%at_____________%' (where _ is a space).

Zach
Mar 6 '09 #3

P: 4
Hi Zach,

I think your onto something there - sounds right.

I'll give it a shot tomorrow and tell you how I go.

Thanks heaps - it's amazing how much I have just learnt if that's all it was.

Cheers,
Matt.
Mar 8 '09 #4

P: 4
Zach,
Helped heaps - working great now.

Thanks so much,

Matt.
Mar 9 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.