sql query - not contain

Posted on 2014-08-13
Last Modified: 2014-08-16
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
Question by:VBdotnet2005
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    you need a 'AND' instead of OR.
    LVL 23

    Accepted Solution

    Just need to re-arrange your logic a little bit:

    FROM table_1
        rcd LIKE '*%'
            OR rcd IS NULL
            OR rcd = 'n'

    Open in new window

    LVL 65

    Assisted Solution

    by:Jim Horn
    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'?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now