nalgeneusa4
nalgeneusa4

Reputation: 11

SQL Server OR statement issue

I am trying to select a row where column C5721_USER_NAME is not starting with "ccs" or C5721_USER_ORIGIN is not starting by "ccs".

If either start with "ccs", I don't want that column.

Here is my query:

SELECT * 
FROM T5707_ISSUES 
WHERE (C5721_USER_ORIGIN NOT LIKE '%ccs%' OR C5721_USER_NAME NOT LIKE '%ccs%')
ORDER BY C5707_ISS_ID 

But my results are for C5721_USER_NAME:

cnewman
ctext
ccskym
lmbsiro

Upvotes: 0

Views: 114

Answers (4)

Sonam
Sonam

Reputation: 3466

Please try this:

SELECT * 
FROM T5707_ISSUES 
WHERE (C5721_USER_ORIGIN NOT LIKE 'ccs%' and C5721_USER_NAME NOT LIKE 'ccs%')
ORDER BY C5707_ISS_ID 

I've replaced or with and, as when you are using OR either of the columns having value starting with those characters can come up.

Upvotes: 1

Joe Taras
Joe Taras

Reputation: 15379

You're wrong logic operator OR instead of AND

SELECT * 
FROM T5707_ISSUES 
WHERE (C5721_USER_ORIGIN NOT LIKE '%ccs%' and C5721_USER_NAME NOT LIKE '%ccs%')
ORDER BY C5707_ISS_ID 

Upvotes: 0

Damon
Damon

Reputation: 3012

If you only want it to search for records that don't start with the value you need to change your query to be:

SELECT * 
FROM T5707_ISSUES 
WHERE (C5721_USER_ORIGIN NOT LIKE 'ccs%' AND C5721_USER_NAME NOT LIKE 'ccs%')
ORDER BY C5707_ISS_ID 

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

You have to use AND

SELECT * 
FROM T5707_ISSUES 
WHERE (C5721_USER_ORIGIN NOT LIKE 'ccs%' AND C5721_USER_NAME NOT LIKE 'ccs%')
ORDER BY C5707_ISS_ID 

Also, to search for something that only starts with ccs you do not have to add an % before it.

Upvotes: 2

Related Questions