Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Or condition not working

Hi Experts,
 Please my sql query.
I need to get all the hire and inter data only but results is wrong any idea for this
issue
SELECT typeName,typeName,stageName,typeID,[stageID] FROM tblcandidate 
  WHERE candidateID='06f94c06364501'
  AND (typeName Like 'hire%' OR typeName Like 'inter%'
 OR  stageName Like 'Hire%'   OR  [stageID] Like 'hire%'
  OR [typeID] Like 'hire%'
  OR [stageName] Like 'inter%' OR [typeName] Like 'inter%'  OR  [stageID] Like 'inter%'
   OR  [typeID] Like 'inter%')

Open in new window


see Results

typeName      typeName      stageName      typeID      stageID
Sourced               Sourced      Internal portal sourced        1563369937876
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

When you are combining AND and OR conditions without braces, OR will bring all records out.
Kindly post the results of the query above and the expected result set to identify the issue..
Avatar of ukerandi

ASKER

@Raja Jegan R
see Results

typeName      typeName      stageName      typeID      stageID
Sourced               Sourced      Internal portal sourced        1563369937876

Expected Results : No results
Kindly double check for any blank spaces or special characters in the data by running the below query..
Below should return your result set expected and if not, then the StageName column contains some special characters.
Also confirm whether StageName column data is case sensitive or not..
  SELECT typeName,typeName,stageName,typeID,[stageID] FROM tblcandidate 
  WHERE candidateID='06f94c06364501'
  AND  [stageName] Like 'inter%'

Open in new window

  SELECT typeName,typeName,stageName,typeID,[stageID] FROM tblcandidate 
  WHERE candidateID='06f94c06364501'
  AND  [stageName] Like 'Inter%' -- To Check column case sensitiveness 

Open in new window

I think this is a case where Normalization shines :
You need a table that stores the different states
Sourced
Internal
Portal
........

Open in new window

You could then filter them without worrying for special chars or what soever
To avoid any case sensitive issue you could use the lower function:
SELECT typeName,typeName,stageName,typeID,[stageID] FROM tblcandidate 
  WHERE candidateID='06f94c06364501'
  AND ((lower(typeName) like 'hire%' OR lower(typeID) like 'hire%' OR lower(stageName) like 'hire%' OR lower([stageID]) like 'hire%')
    OR (lower(typeName) Like 'inter%' OR lower(typeID) like 'inter%' OR lower(stageName) like 'inter%' OR lower([stageID]) like 'inter%'))

Open in new window

Of course there are better ways to normalize your data, for starters I will create all these types under id, name description table and just use the id to identify your hire and inter data.
You are getting the proper results for your criteria:
For criteria:  
OR [stageName] Like 'inter%'
and the record has:
stageName
Internal
Why do you think this is incorrect?  You're looking for a record with [stageName] starting with "Inter" and "Internal" meets that criteria.

Think again and get back to us....
ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will check and updated soon
Are you wanting your comparisons to be case-sensitive, where "inter" does NOT equal "Inter"?
This is called a "binary comparison".  Is that what you are wanting?