?
Solved

SQL JOIN?

Posted on 2007-07-30
6
Medium Priority
?
151 Views
Last Modified: 2010-03-19
How do I take data from one field and join it with data from another field, plus provide some addtional text around that data. So as an example:

TableUserName                 TableUserNumber                  

Joel                                     555-1212
John                                    555-1213
Mary                                    555-1214


The result needs to look like the below while adding the following characters, "=" and "!"

Joel=555-1212!
John=555-1213!
Mary=555-1214!

Joel
0
Comment
Question by:Joel_Sisko
6 Comments
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 total points
ID: 19597028
SELECT TableUserName + '=' + TableUserNumber + '!'
FROM SomeTable
0
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 600 total points
ID: 19597567
Plus only adds numbers. You need
SELECT CONCAT( TableUserName , '=' , TableUserNumber , '!')
FROM SomeTable
0
 
LVL 18

Accepted Solution

by:
Sham Haque earned 800 total points
ID: 19598505
>Plus only adds numbers

er - no it doesn't. in T-SQL, it is the concatenation operator.
beware NULLs though, as by default, where a NULL value is present anywhere in the items being concatenated, this will result in a NULL result.

2 options to avoid this pitfall:

1. use SET CONCAT_NULL_YIELDS_NULL OFF for the session (and turn it back on after your SELECT

2. Use ISNULL to explicitly return a value
eg SELECT ISNULL(TableUserName,'') + '=' + ISNULL(TableUserNumber ,'') + '!'
FROM SomeTable
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Author Comment

by:Joel_Sisko
ID: 19611195
gbshaaq,

Option 2 of your response (ISNULL), if I understand you correctly, that if there is a "null" in a field the SQL statement will not return anything. So in the example below:

TableUserName                 TableUserNumber                  

Joel                                     <null>
John                                    555-1213
Mary                                    555-1214

The result would be:

John=555-1213!
Mary=555-1214!





0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19611799
The result would be:

<null>
John=555-1213!
Mary=555-1214!
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 19721153
>>Plus only adds numbers

>er - no it doesn't. in T-SQL, it is the concatenation operator.

oops. Sorry, I thought i was in the mysql group. My bad.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, we’ll look at how to deploy ProxySQL.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

864 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