?
Solved

SQL concat in Where clause

Posted on 2010-09-20
12
Medium Priority
?
617 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
  • 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
Technology Partners: 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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

807 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