?
Solved

T_SQL: joining tables on nvarchar and int columns

Posted on 2013-06-03
4
Medium Priority
?
1,032 Views
Last Modified: 2013-06-07
We'd like to join 2 tables on common column and get the data from both of them. The problem is that even though the data in the columns is similar, the datatype is not the same. One is nvarchar and the other is int. Some data cannot be converted to int.
What is the best way to write the query in order to get matching rows from both tables?
Thank you in advance.
0
Comment
Question by:quasar_ee
[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 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 39217666
Something like this:
FROM    TableA a
        INNER JOIN TableB b ON CASE PATINDEX('%[^0-9]%', a.nvarcharcolumn)
                                 WHEN 0 THEN CAST(a.nvarcharcolumn AS int)
                                 ELSE NULL
                               END = b.integercolumn 

Open in new window


Don't use ISNUMERIC() that is a bad idea.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 39217992
convert both to the nvarchar is then best because to convert int error occurs
use CAST or convert both data types to nvarchar.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39217998
You could try in the other direction, i.e. convert int to nvarchar which should always work thus avoiding the patindex. (Mind you it's a pity you have to do this at all.)

select
*
from TableA a
inner join TableB b on a.nvarcolumn = convert(nvarchar,b.intcolumn)

which of these 2 columns is indexed? maximizing use of indexes may influence the choice you make.
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 39225516
CAST the NVarchar column to Int or CAST INT to NVARCHAR in where or Join (ON part).

It will work for you.
0

Featured Post

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!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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