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