Solved

T_SQL: joining tables on nvarchar and int columns

Posted on 2013-06-03
4
952 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
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 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 48

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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