SqlServer: Like vs. “=” for matching strings

Whenever I write a stored procedure for selecting data based on string variable (varchar, nvarchar, char) I would have something like:

procedure dbo.p_get_user_by_username(
    @username      nvarchar(256)
as
begin
    select
    	u.username	
    	,u.email
    	--,etc
    from
    	sampleUserTable u
    where
    	u.username = @username
end

So in other words to match the record I would have

u.username = @username

But sometimes I come across code that would use LIKE in place of =

u.username like(@username)

When would you use it? Shouldn't that be used only when you need some wildcard matching?

EDIT

Thanks for the answers.

I think that I need to clarify that what I was really trying to ask was: if there could be a situation when it was preferred to use like in place of "=" for exact string matching. From the answers I could say that there would not be. From my own experience even in situations when I need to ignore e.g case, and leading and ending spaces i would use ltrim, rtrim, lower on both strings and then "=". Thanks again for your input.

Answers


You are correct. There is no benefit in using LIKE unless you are doing wild card matching. In addition, using it without wildcard could lead to the use of an inefficient queryplan.


Need Your Help

Custom image sizes for custom field media uploads in WordPress

image wordpress custom-fields

When we simply want to show an image defined by a custom field (say "feature-image") we'd use:

How to get the file item right-clicked in Finder on OSX

objective-c osx finder contextmenu

I'm writing a plugin via mach_inject to add an item to Finder context menu. I have successfully add it by hooking NSMenu. But now i need to get the item that is right-clicked.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.