Solved

a tsql question

Posted on 2013-01-29
3
224 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
INSERT DATE FROM STRING COLUMN 18 63
Email Notifications for SQL 2005 9 36
Need help how to find where my error is in UFD 6 46
tempdb log keep growing 7 43
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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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