Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The theory behind this , i wanna know for what reason, employees have terminated and then , I inner join 2 table (Termination Reasons with Users) were the two talk to the each other and Termination Reasons table contains 2 column (Id (int) , Name(varchar)) and the other one , Users Table contains 1 column (EmploymentStatusId (int)) .


I can use EmploymentStatusId(int) from tbl Users with Id (int) from another table wich is Termination Reasons beacuse , both of them have same data type (int):
SELECT 
u.EmploymentStatusId AS Status

FROM 
x AS p INNER JOIN y AS u ON u.Id=x 
INNER JOIN x AS t ON t.Id = y
WHERE r=214 order by g;


But when i try using EmploymentStatusId (int) from tbl Users with Name (varchar) from tbl Termination Reasons , i got problem:
SELECT
w AS Status

FROM 
s AS p INNER JOIN s AS u ON u.q=v
INNER JOIN v AS t ON t.Name = n
WHERE f=214 order by h;


And then Error :
Conversion failed when converting the varchar value 'TerminatedByCompany' to data type int.

i know why ,because they have different data type , therefore they can not talk to the each other.
but i tried also to solve this with Convert but idont know ,why or how is not working.
Can someone please help or point me in the right direction. Thanks.
The Query ,where i tried with CONVERT ,its look like this:
SELECT
CONVERT(INT, CONVERT(VARCHAR(12), t.Name)) as Status

FROM 
s AS p INNER JOIN t AS u ON j=u
INNER JOIN z AS t ON z = z
WHERE z=214 order by  b;


What I have tried:

i tried also to solve this with Convert but idont know ,why or how is not working.Can someone please help or point me in the right direction. Thanks.
Posted
Updated 28-Feb-17 2:16am
v2
Comments
Richard MacCutchan 28-Feb-17 6:36am    
You cannot convert a string of alphabetic text to an int, it makes no sense.
Man Down 28-Feb-17 6:42am    
i know and your right
Richard MacCutchan 28-Feb-17 6:45am    
So what do you expect by posting this question?
Man Down 28-Feb-17 6:51am    
if is there anything for, convert varchar to int . but i find out now.
tnx for your help and time
Richard MacCutchan 28-Feb-17 7:11am    
Like I said, the question makes no sense. What value would you expect from "TerminatedByCompany"?

1 solution

Look at your code:
SQL
INNER JOIN dbo.TerminationReasons AS t ON t.Name = u.EmploymentStatusId
If they have different datatypes, where one is a name (currently "TerminatedByCompany") and the other is an integer ID value, then nothing you can do will make them pass a comparison. SQL is - rightly - saying "don't be so silly, I can't compare these as they aren't even close to the same"
Rather than trying to convert and bodge round this, you need to look more closely at your tables, and work out what values you should be using - I'm guessing that Name is completely the wrong column for the JOIN clause, but since I can't access your DB I have no idea what you do need: it's probably an integer though, rather than a NVARCHAR and should probably have a FOREIGN KEY relationship already established.
 
Share this answer
 
Comments
Man Down 28-Feb-17 6:48am    
that was i Exactly thinking but my colleagues put pressure on me at this 2 table have relation But Now , im 100% sure, there are nothing between them.

Tnx for your help and comment OriginalGriff
OriginalGriff 28-Feb-17 6:51am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900