Solved

a tsql question

Posted on 2013-01-29
3
213 Views
Last Modified: 2013-01-31
I can select records with the following statement -

select [id], [location], fn as 'firstname', ln as 'lastname'
from dbo.mytable
where fn like 'Mike%'

I need to change to the following query, why I cannot select any record?

declare @ttt varchar(10)
set @ttt = '''' + 'Mike' + '%' + ''''
--print @ttt
select [id], [location], fn as 'firstname', ln as 'lastname'
from dbo.mytable
where fn like @ttt

when I print  @ttt, it has the value 'Mike%'. I'm wondering why I cannot select any record after I replace 'Mike%' with a variable having the same value in the where clause.

Please help. Thanks.
0
Comment
Question by:minglelinch
3 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 250 total points
ID: 38831379
Hi,

Try this:

declare @ttt varchar(10)
set @ttt = 'Mike%'
--print @ttt
select [id], [location], fn as 'firstname', ln as 'lastname'
from dbo.mytable
where fn like @ttt

Open in new window


Giannis
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 38831568
You should not enclose variable values in quotes.

So, instead of this:

declare @ttt varchar(10)
set @ttt = '''' + 'Mike' + '%' + ''''  -- name to be searched for is 'Mike%': the single quotes would have to be in the person's fn to match

It should be this:

declare @ttt varchar(10)
set @ttt = 'Mike' + '%'  -- string to be searched for is Mike%
0
 
LVL 1

Author Closing Comment

by:minglelinch
ID: 38840099
Thank you for the answer.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now