Solved

SQL concat in Where clause

Posted on 2010-09-20
12
614 Views
Last Modified: 2012-05-10
How can I accomplish this?

Select emailAddress
From UserList
Where (fname||" "||lname) =  'John Doe'
0
Comment
Question by:Rick
[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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 11

Expert Comment

by:aelliso3
ID: 33717841
Select emailAddress
From UserList
Where (fname + ' ' + lname) =  'John Doe'
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33717850
aren't the + enough:

Select emailAddress
From UserList
Where (fname + " " + lname) =  'John Doe'
0
 
LVL 13

Author Comment

by:Rick
ID: 33717875
aelliso3 - I get nothing
emoreau - Invalid column name ' '.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:aelliso3
ID: 33717896
Try this:
 
Select emailAddress
From UserList
Where fname = 'John' and lname = 'Doe'
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33717910
can you provide more information on what you are doing because the error makes no sens to me !
are you running this query from SSMS?
0
 
LVL 11

Expert Comment

by:aelliso3
ID: 33717932

emoreau ... yours had 2 double quotes instead of 2 single quotes ...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33717989
doh!
0
 
LVL 13

Author Comment

by:Rick
ID: 33718022

I have this table:  

tblItems:  
ItemID, CreatedBy, CreatedDate
001, John Doe, 9/10/2010 10:15:00 PM
 

And this table:
 tblUsers:
FName, LName, Email
John, Doe, jdoe@email.com
 
I want to select the email address from tblUsers of whom created ItemID 001. In this case John Doe's email.
 
0
 
LVL 13

Author Comment

by:Rick
ID: 33718041
I'm using vb, I can split 'John Doe' and do what you said above:
 
Select emailAddress
From UserList
Where fname = 'John' and lname = 'Doe'
But I was hoping to get this accomplished directly from my SQL query.
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 250 total points
ID: 33718169
Check the attached script.

Raj
create table #tblUsers
(
	FName  varchar(25), 
	LName		varchar(25), 
	Email	varchar(25)
)

insert into #tblUsers
select 'John', 'Doe', 'jdoe@email.com '


select * from #tblUsers where FName +  ' ' + LName =  'John Doe' 

drop table #tblUsers

Open in new window

0
 
LVL 11

Assisted Solution

by:aelliso3
aelliso3 earned 250 total points
ID: 33718211
Select *
from tblItems
     INNER JOIN tblUsers
          ON tblItems.CreatedBy = tblUsers.FName + ' ' + tblUsers.FName

Then you can use any field to get the values you want
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33759931
Nice to help you :-)

It's my 400th accepted answer!

Raj
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

691 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