Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I LEFT JOIN between column types INT and VARCHAR?

I have been given the task to look into a issue with a MSSQL Query and I'm not SQL pro! It's not my forte´.

I have 2 tables in 2 different databases; ignore table references, as I have stripped them out of this query. My issue is PERSON_CODE is a integer value in one table and VARCHAR in another, I will be running this query and parsing the data directly into the Users table whilst hashing passwords in PHP.

However, because some of the fields in the Users table are character names like Jane, John and all values in the Students are integer only I receive a error because of data types.

Below is the query I have built so far - it's very simple:

SELECT Students.PERSON_CODE, Students.PASSWORD
FROM Students
LEFT JOIN users 
ON Students.PERSON_CODE = users.username
WHERE Students.PERSON_CODE > '0' 
AND users.username IS INTEGER <- How do i do this. 

I need to know if there is a way of ignoring the fields which are not integers in the Users table so I can get only integer results from the Students table.

Below was the full solution. Thank you Preveen.

SELECT Students.PERSON_CODE, Students.PASSWORD
FROM Students
LEFT JOIN users 
ON Students.PERSON_CODE = CASE 
                          When ISNUMERIC(users.username) = 1 THEN users.username
                          END
WHERE Students.PERSON_CODE > '0' 

NEW UPDATES

I have made some changes because I want the query to show only rows which are not in both tables using PERSON_COde/username as the identifier.

SELECT Students.PERSON_CODE, Students.PASSWORD
FROM Students
LEFT JOIN users 
ON cast(Students.PERSON_CODE as varchar(15)) = users.username
WHERE users.username = cast(Students.PERSON_CODE as varchar(15))

In a way i need a full outter join essentially I think.

Table Students_________ Table users
PERSON_CODE____________ username
1______________________ 1
2______________________ 2
3______________________ 3
4______________________
5______________________
6______________________
7______________________
8______________________

With this query I then want to display the results 4,5,6,7,8

like image 676
Steve Church Avatar asked Jan 23 '14 08:01

Steve Church


People also ask

Can we join int and varchar in SQL?

In the earlier version of SQL Server, we usually use CONVERT function to convert int into varchar and then concatenate it. Given below is the script. Solution 2: In this solution, we will use CONCAT function (a newly shipped function in SQL Server 2012) to convert int into varchar then concatenate it.

Can you join on varchar?

It's certainly possible to use a varchar as a key field (or simply something to join on).

Can we join two columns with different names?

Note the following when using UNION in SQL: All SELECT statements should list the same number of columns. The corresponding columns must have the same data type. The corresponding columns can have different names, as they do in our example.

Can we apply join on multiple columns in SQL?

If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).


1 Answers

This seems the best solution to me:

SELECT Students.PERSON_CODE, Students.PASSWORD
FROM Students
LEFT JOIN users 
ON cast(Students.PERSON_CODE as varchar(10)) = users.username
WHERE Students.PERSON_CODE > 0 

This way you don't need to check if username is an integer. You simply convert PERSON_CODE to a varchar(10) before comparing and problem is solved.

like image 86
t-clausen.dk Avatar answered Oct 19 '22 23:10

t-clausen.dk