Solved

SQL concat in Where clause

Posted on 2010-09-20
12
600 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
Comment Utility
Select emailAddress
From UserList
Where (fname + ' ' + lname) =  'John Doe'
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
aren't the + enough:

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

Author Comment

by:Rick
Comment Utility
aelliso3 - I get nothing
emoreau - Invalid column name ' '.
0
 
LVL 11

Expert Comment

by:aelliso3
Comment Utility
Try this:
 
Select emailAddress
From UserList
Where fname = 'John' and lname = 'Doe'
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility

emoreau ... yours had 2 double quotes instead of 2 single quotes ...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
doh!
0
 
LVL 13

Author Comment

by:Rick
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Nice to help you :-)

It's my 400th accepted answer!

Raj
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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

14 Experts available now in Live!

Get 1:1 Help Now