Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

sql query - not contain

I want to query and get only itemno that does not starts with '*' (like *100037-000), and RCD  not 'y'  . This column is char(25).  It still return * itemno in front.
     
     SELECT ITEMNO FROM table_1 WHERE rcd not like ('*%') or rcd is not null or rcd <> 'n'
     
     select distinct(rcd) from tables_1
     results : Y, N, NULL
0
VBdotnet2005
Asked:
VBdotnet2005
2 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
you need a 'AND' instead of OR.
0
 
nemws1Commented:
Just need to re-arrange your logic a little bit:

SELECT ITEMNO
FROM table_1
WHERE NOT (
    rcd LIKE '*%'
        OR rcd IS NULL
        OR rcd = 'n'
)

Open in new window

0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Hmm...  Working for me in SQL 2012..
CREATE TABLE #tmp (val varchar(10))
INSERT INTO #tmp (val) VALUES ('*abc'), ('123'), ('Bob'), ('wackadoo')

SELECT * FROM #tmp WHERE val NOT LIKE '*%'
SELECT * FROM #tmp WHERE LEFT(val, 1) <> '*'

Open in new window

>RCD  not 'y'
>WHERE .. rcd <> 'n'
Perhaps you meant WHERE .. rcd <> 'Y'?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now