15

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steve Church
  • 344
  • 1
  • 3
  • 12

4 Answers4

21

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.

t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
2

Try this:-

ON Students.PERSON_CODE = CASE 
                          When ISNUMERIC(users.username) = 1 THEN users.username
                          END

where Clause:

And ISNUMERIC(users.username) = 1
praveen
  • 12,083
  • 1
  • 41
  • 49
1

Sometimes using ISNUMERIC() won't work too well, as comma and decimal point would also be considered as numeric. I prefer to use TRY_CONVERT(int, a.var_key) = b.int_key, as TRY_CONVERT() will give NULL to ignore the non-integer values needed for the JOIN to work.

Irawan Soetomo
  • 1,315
  • 14
  • 35
0

You can use like this also, optimization using derived table join will avoid full table scan in sql join process in a bigger table.

SELECT Students.PERSON_CODE, Students.PASSWORD
FROM Students
LEFT JOIN
(SELECT users.username FROM users 
WHERE ISNUMERIC(users.username)= 1) As Myusers
ON Students.PERSON_CODE = Myusers.username
WHERE Students.PERSON_CODE > 0 
Sundar
  • 4,580
  • 6
  • 35
  • 61