Solved

a tsql question

Posted on 2013-01-29
3
223 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:
Scott Pletcher 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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