Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Join on unsimilar columns

Author  Topic 

major-d
Starting Member

2 Posts

Posted - 2011-01-21 : 04:26:54
Hi all,

I have been trying to join two tables on unsimilar columns, the column in table 1 has an ID field and the column in table 2 has the same ID fields with a prefix of "vm-" and "host-" (for the mean time I am only interested in joining on the [vm-]ID columns.

ID in table 1 is an int and ID in table 2 is a nvarchar so I have casted ID in table 1.

My example

Table 1

ID
_____
66016
66017
66018
66019

Table 2

ID
_____
vm-66016
vm-66017
vm-66018
vm-66019

My Statement


select Col1A.Col1B,Col1C

from table1 vm
LEFT JOIN table2 va
ON CAST(vm.ID AS nvarchar (15))LIKE 'vm-'+va.MO_ID

The values returned from the joined table are NULLs which would suggest my ON is not working correctly.


Any help would be greatly appreciated as I have not had to join two tables where there wasnt a common feature before.

Thanks,

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-01-21 : 04:59:11
ON 'vm-' + CAST(vm.ID AS nvarchar (15)) = va.MO_ID



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

major-d
Starting Member

2 Posts

Posted - 2011-01-21 : 05:07:31
ah - i was doing 'vm-'+ on the wrong side of the link...

so in fact i was looking for vm-vm-[ID] hence no met conditions


Great - thank you so much for you assistance
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-01-21 : 05:09:01
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -