user2308890
user2308890

Reputation:

Table Join returns Null with matching fields in SQL Server 2008

I have a weird problem. I'm trying to run this code but it keeps returning no matches. This should work. The LanguageId exists in both tables. The only thing I can think of is that there is the field type is nvarchar.

SELECT Users.LanguageId
FROM Users
JOIN Locale ON Users.LanguageId = Locale.LanguageId

You can download the test database at http://www.mediafire.com/download/3eute31s35tygw2/TEST.zip. I'm guessing there is something with the way the 'en-us' is saved to Users.LanguageId. If delete the value and manually type in 'en-us' then it works.

Upvotes: 0

Views: 45

Answers (2)

Robert
Robert

Reputation: 25753

You have to change en-us to en_us, try below solution

SELECT Users.LanguageId
FROM Users
JOIN Locale ON Users.LanguageId = REPLACE(Locale.LanguageId,'-','_')

or better way is to fix data in your table:

update Locale
set LanguageId = REPLACE(LanguageId,'-','_')

Upvotes: 1

Thanos Markou
Thanos Markou

Reputation: 2624

LanguageId on one table is en-us and on the other is en_us. That's why they dont join correctly.

Upvotes: 3

Related Questions