Solved

a tsql question

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

18 Experts available now in Live!

Get 1:1 Help Now