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
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.
It's certainly possible to use a varchar as a key field (or simply something to join on).
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.
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 ).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With