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 varchar and INT

Author  Topic 

KlausEngel
Yak Posting Veteran

85 Posts

Posted - 2010-10-16 : 17:00:43
I have 2 tables that I would like to join on the ID column, however the second table's ID column is of datatype varchar and the values are stored like: 0001, 0002, etc.., 1001, 1002
The ID column in table 1 are stored like 1,2,etc.., 1001, 1002. So I get mismatches until I hit ID 1000. Any help is appreciated.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-16 : 17:56:30
If the second table's Id is of datatype INT then there should be no problem because an implicit convert will make them both to INT while comparing.
But if the second table's Id is of datatype varchar too then just do an explicit convert.

on convert(int,t1.Id) = convert(int,t2.Id)


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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-17 : 03:02:18
anyways its not good to use varchar datatype for fields when you're sure that it stores only numeric data. I assume you've inherited this if not strongly consider making it numeric data type.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -