Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

a tsql question

Posted on 2013-01-29
3
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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