+ Reply to Thread
Results 1 to 9 of 9

Thread: Search for incorrect pattern

  1. #1
    Barn Frequenter Flam is on a distinguished road Flam's Avatar
    Join Date
    Dec 2008
    Posts
    100
    Rep Power
    2

    Search for incorrect pattern

    I have used charindex and patindex but I'm wondering if anyone has a good way to deal with the problem I'm trying to figure out.

    If you look at the following data:

    Code:
    CREATE TABLE #MYTEMPTABLE (COL1 INT PRIMARY KEY, COL2 VARCHAR(30))
    
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (1, 'YES PCA')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (2, ' YES PCA')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (3, 'NO PCA')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (4, ' NO PCA')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (5, 'PCA YES')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (6, 'PCA NO')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (7, ' YES ')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (8, ' NO')
    INSERT INTO #MYTEMPTABLE (COL1, COL2) VALUES (9, ' Was PCA APPROVED')
    
    SELECT * FROM #MYTEMPTABLE
    
    The values returned are:
    Code:
    1    YES PCA
    2     YES PCA
    3    NO PCA
    4     NO PCA
    5    PCA YES
    6    PCA NO
    7     YES 
    8     NO
    9     Was PCA APPROVED
    
    The values for ID's (Col1) 1 and 3 are the "correct" patterns I'm looking for.
    Given this example, if I want to search for anything where Col2 is not in
    ('YES PCA', 'NO PCA'), I could write a query stating:

    SELECT *
    FROM #MYTEMPTABLE
    WHERE (LEFT(COL2,7)<>'YES PCA' OR
    LEFT(COL2,6)<>'NO PCA')

    but the problem is that it just returns all 9 rows when I would only want to see those rows that "weren't correct"

    I'm looking to say something like:

    Select case when (LEFT(COL2,7)<>'YES PCA' OR
    LEFT(COL2,6)<>'NO PCA') then 1 else 0 end as 'NEEDS TO BE FIXED'

    FROM #MYTEMPTABLE

    Hopefully this is making sense...the columns where "PCA" doesn't show up at all or if "NO" or "YES" don't show up in COL2 kind of baffle me.

    Thanks!
    Craig

    EDIT: The results returned look trimmed in my POST...obviously records 2,4,7,8 and 9 have leading spaces in the result set
    Last edited by Wolffy; February 3rd, 2010 at 01:17 PM. Reason: Added CODE tags, please do so in the future

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    Not sure I see why you are using the LEFT function here. Would not this query return what you are looking for?
    Code:
    select * from #myTempTable
    where COL2 NOT IN ('YES PCA', 'NO PCA')
    
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Barn Frequenter Flam is on a distinguished road Flam's Avatar
    Join Date
    Dec 2008
    Posts
    100
    Rep Power
    2

    Ah, my apologies...yes, I understand the confusion.

    It's because there can be other strings after 'YES PCA' or 'NO PCA'
    One of those two patterns always have to be in COL2 and they always have to start at position 1.

    So for example:

    'YES PCA DOES NOT NEED ASSISTANCE'
    could be an acceptable data set for that column.

    Given your query, it would falsely bring up this example since it is NOT IN
    ('YES PCA', 'NO PCA')

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    In that case, try:
    Code:
     select *
     from #myTempTable
     Where col2 not like ('YES PCA %') 
       and col2 not like ('NO PCA %') 
       and col2 not in ('YES PCA', 'NO PCA')
    
    The slightly more efficient
    Code:
       SELECT *
    FROM #MYTEMPTABLE
    WHERE NOT (LEFT(COL2,7)='YES PCA' OR
    LEFT(COL2,6)='NO PCA')
    
    would work with your example data, but would miss 'YES PCASSISTANT'
    Last edited by Wolffy; February 3rd, 2010 at 01:45 PM.
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    173
    Blog Entries
    8
    Rep Power
    5

    I think the real issue here is that you are treating the contents of a column as if it may contain more than one kind of a value. While it's possible to write awkward searches even in such cases, it's a violation of relational data normalization to treat a column as non-atomic ("single valued"). You are searching for pieces of a value. Perhaps you are forced to deal with the data you are given, but if you are designing a database, you should try to avoid this by having columns for PCA (Yes or No) and additional qualifiers. That's the reason for the normalization rules, to permit simple queries to retrieve the data you want.
    question = 2B || !2B

  6. #6
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    Indeed Don;
    Generally, if you find it hard to write a query or need to handle lots of 'exceptions' in complicated Where clauses, its a good indication that your DB Design is flawed. Ideally, working with YES/NO type values, one would use a bit field; and if there are different 'flavors' of Yes/No, then additional fields would be added (to basically say the same thing as Don).

    Also, one should be careful to not allow values such as <blank>Yes to be stored in the first place.
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

  7. #7
    Barn Frequenter Flam is on a distinguished road Flam's Avatar
    Join Date
    Dec 2008
    Posts
    100
    Rep Power
    2

    Don and Wolffy,

    Please talk to our DB designer!!!! *smiles* Yes, I agree...some of it is their fault, some of it is us trying to be innovative in using our tools where the database (which was provided) lacks some of our needed functionality.

    I'm sure Wolffy remembers very well an even more convoluted situation that our organization had a few months ago with...*ahem* exceptions

    We set up a lot of data integrity queries to catch things that really should be normalized, but we have no ability to do (since we're not the developers).

    We have issues where users input spaces by accident, in which case we usually write integrity queries that look like:

    SELECT CASE WHEN LEFT(COL2,1)=' ' THEN 1 ELSE 0 END AS 'FIX_LEADING_SPACE

    FROM MYTABLE WHERE SOME CONDITIONS EXIST

    So yes, I agree with both of you...just can't do anything about it; hence the need for pattern searches.

    Wolffy, I did try both of those queries before I wrote my thread, just thought there might be another way of going about it.

    Thanks so much!
    Craig

  8. #8
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    Craig;
    Yup, I remember that fondly(?). Figured it was another case of 'inherited DB design'.

    Could you update the table to strip out the leading spaces on some 'maintenance cycle', or would that bollocks up a process someplace else?
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

  9. #9
    Barn Frequenter Flam is on a distinguished road Flam's Avatar
    Join Date
    Dec 2008
    Posts
    100
    Rep Power
    2

    Hey Wolffy,

    Yup...I run an update query for most of these types of things...it would be great to have it normalized, but in lieu of that, I run multiple queries from time to time such as:

    UPDATE Table
    Set Col2=UPPER(Col2);
    UPDATE TABLE
    Set Col2=LTRIM(Col2)

    etc etc

    The issue is easily finding the issues so we can apply the proper update queries.

    I can set up user fields with drop down menus which would be great! The problem is, those custom fields don't show up to the users in the places they need them.

    Anyway, what I'm doing now works, just thought there was a better, more scalable way to address the issue as tables become more populated.

    Thanks!

+ Reply to Thread

Similar Threads

  1. Search from help
    By Colper in forum Microsoft Access
    Replies: 7
    Last Post: December 22nd, 2009, 05:26 PM
  2. SQL Search in VBA
    By Colmag in forum Microsoft Access
    Replies: 9
    Last Post: July 27th, 2009, 03:04 PM
  3. SQL2000 Incorrect syntax
    By Rebelle in forum SQL Development
    Replies: 2
    Last Post: April 30th, 2009, 09:00 AM
  4. incorrect syntax
    By todd2006 in forum ASP Development
    Replies: 5
    Last Post: February 13th, 2009, 12:55 PM
  5. Javascript Regex Pattern for decimal number(18,5)
    By guddu in forum JavaScript Programming
    Replies: 12
    Last Post: January 27th, 2009, 11:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO