user1738575
user1738575

Reputation: 11

Seemingly simple query throws error

I have 2 tables for a lab I'm working on. Table 1 is a list of job titles, with a description, title, and status field, along with a index value that I need to be able to join with the second table, Employees. The query below seems simple enough but it's throwing back an error:

SELECT e.FirstName, e.LastName, e.Salary, jt.JobTitle, jt.JobDesc, jt.[Status]
From Employee e
JOIN Job_Title jt
ON e.EEO-1Class = jt.EEO-1Class
WHERE e.Salary BETWEEN 10000 AND 40000

It blows up on e.EEO-1Class saying "An expression of non-boolean type specified in a context where a condition is expected, near 'Class'.

The fields I'm joining are int fields already so I'm confused about what I'm missing. I'm sure this is one of those "duh" answers :)

Upvotes: 1

Views: 77

Answers (3)

freefaller
freefaller

Reputation: 19953

You need to put the name of the field into square brackets.

So change...

ON e.EEO-1Class = jt.EEO-1Class

Into

ON e.[EEO-1Class] = jt.[EEO-1Class]

If your field or table name contains just alphanumeric characters (ie A-Z and 0-9) and the underscore _ character, then you can use it without square brackets ([ and ]).

But as soon as you have a different characters in there (and one that can easily be confused by SQL server, such as -) then you need to place the field/table into square brackets.

However, what I would recommend above everything else is that you only ever use alphanumeric characters (and the underscore _ character) for your field and table names, as it is considered better practise, and will remove this type of thing happening again for you

Upvotes: 3

John Woo
John Woo

Reputation: 263693

escape your column names: EEO-1Class and EEO-1Class with brackets. The server performs substraction because of - sign.

SELECT e.FirstName, e.LastName, e.Salary, jt.JobTitle, jt.JobDesc, jt.[Status]
From Employee e
JOIN Job_Title jt
ON e.[EEO-1Class] = jt.[EEO-1Class]     -- << this two columns.
WHERE e.Salary BETWEEN 10000 AND 40000

Upvotes: 2

Jay
Jay

Reputation: 2129

I think it may be the '-' in the column names. Try putting it inside [] brackets.

Upvotes: 2

Related Questions