Need a way of finding special characters in data using SQL query - sql

I am trying to find special characters in any of my fields that are not in the range of a-zA-Z0-9. However if I try this query:
select Name from table where Name like '%[?]%'
I get two records:
???? ?????
Fixed?????
Which is what I want. However, since I don't know what the special chars will be I need to use an exclusion of data that has mixed characters:
select Name from table where Name NOT like '%[a-zA-Z0-9]%'
Since this excludes all records with a-zA-Z0-9 I only get:
???? ?????
But I also need to get the 'Fixed?????' result. I need to get the data that has the special character merged into it.
I am bit at a loss as how to do this. I've seen this done with shell scripts or 'vi' (LIST), but in SQL that's not so easy.
Has anyone out there solved this?

Try this code:
select Name from table where Name like '%[^0-9a-zA-Z ]%'

Thank you for replying. I had tried your suggestions but I was still getting more results. However, it looks like you can get very specific with the exclusion. Eventually I ended up adding results from the data I got.
Like this:
select Name from table where Name LIKE '%[^0-9a-zA-Z() -._/\:=,]%'
This finally gave me what I was looking for. Although new issue I have now is how to suppress the [] brackets which apparently also are found in the data:
???? ?????
HP PCI 10/100Base-TX Core [100BASE-TX,FD,AUTO,TT=1500]
Fixed?????
Adding those brackets into the query breaks the array boundary:
'%[^0-9a-zA-Z() -._/\:=,**[]**]%'
However, this is something I can handle. As long as I am not getting "all" the data.

LIKE '%[^0-9a-zA-Z]%'
numbers (0-9), lowercase alphas (a-z), uppercase alphas (A-Z). The "^" makes that a "NOT" one of these things

Related

LIKE or REGEX trouble

I'm tyring to pull everything out of a column when the word assistant is present with something like below:
SELECT *
FROM table
WHERE field LIKE '%assistant%'
but this is not working. I am not getting an error and it is filtering the data somehow but it appears to actually be filtering my requested values OUT (I have verified the values exist). Can anyone explain what is wrong with attempt or how I can do this in regex with REGEXP_LIKE?
For regex, the pattern would be anythingASSISTANTanything
Does this do what you want?
WHERE LOWER(field) LIKE '%assistant%'

How do I code Google query variables referring to text with an apostrophe in it?

Although I can find solutions to errors caused when a simple text value in a Google query contains an apostrophe, I cannot yet find a solution to when a variable in a Google query might (or might not) refer to a text value that contains an apostrophe.
I am using a simple code to extract a value (column B) related to a list of schools (column A), some of which have apostrophes in their names.
query(SchoolsData_db,"select B where A = '"&$A13&"' label B ''")
For all of the schools with apostrophes I get a #VALUE! error.
I am wondering if there is code I can run against the variable itself ('"&$A13&"') to handle the possible apostrophe. I know I can create a "cleaned name" column corollary to the school names, and then a reverse lookup to restore the names with my results table, but I am hoping the problem can be solved in each calculation instead.
You might try creating a variable first and concatenating that to the query string.
OK - interpreting the suggestion I got, I created and included a text ID column in the schools data (ex: S0001, etc.) and then I used that ID as my variable in the query. All is well. Thanks. I hope others can find this if they are similarly perplexed.

SQL - How do I find the exact string in a list of strings that can be part of another string in the list?

The question sounds confusing but I have a column of data that is divided up by characters |~*~|. I am trying to find data based on the strings in between these characters. The line of characters doesn't start with the delimiter but it does end with it.
e.g. Product Developer|~*~|Technician|~*~|
The issue I've run into is the following:
Product Developer|~*~|Technician|~*~|Lead Product Developer|~*~|
If I search for WHERE T.COLUMN LIKE '%Lead Product Developer%' it is fine I get that column, but if I search for WHERE T.COLUMN LIKE '%Product Developer%' then I'll get where Lead Product Developer and Product Developer since it is part of the string. How can I avoid this and only get exactly the string I am looking for.
Here is a snippet of what I have:
SELECT
T.COLUMN,
FROM TABLE1 T
WHERE T.COLUMN LIKE '%Product Developer%'
First, your data structure is wrong, wrong, wrong. The correct SQL way of representing this relationship is with another table, with one row per whatever and item in the list. In addition, Oracle offers JSON structures and nested tables. So, there is no shortage of ways to do this right.
Sometimes, we are stuck with other people's really bad design decisions. In this case, you can use like in a clever way:
WHERE '|~*~|' || T.COLUMN LIKE '%|~*~|Product Developer|~*~|%'
Because column ends with a delimiter, you don't need to add it to the end again, only to the beginning.

SQL Contains - only match at start

For some reason I cannot find the answer on Google! But with the SQL contains function how can I tell it to start at the beginning of a string, I.e I am looking for the full-text equivalent to
LIKE 'some_term%'.
I know I can use like, but since I already have the full-text index set up, AND the table is expected to have thousands of rows, I would prefer to use Contains.
Thanks!
You want something like this:
Rather than specify multiple terms, you can use a 'prefix term' if the
terms begin with the same characters. To use a prefix term, specify
the beginning characters, then add an asterisk (*) wildcard to the end
of the term. Enclose the prefix term in double quotes. The following
statement returns the same results as the previous one.
-- Search for all terms that begin with 'storm'
SELECT StormID, StormHead, StormBody FROM StormyWeather
WHERE CONTAINS(StormHead, '"storm*"')
http://www.simple-talk.com/sql/learn-sql-server/full-text-indexing-workbench/
You can use CONTAINS with a LIKE subquery for matching only a start:
SELECT *
FROM (
SELECT *
FROM myTable WHERE CONTAINS('"Alice in wonderland"')
) AS S1
WHERE S1.edition LIKE 'Alice in wonderland%'
This way, the slow LIKE query will be run against a smaller set
The only solution I can think of it to actually prepend a unique word to the beginning of every field in the table.
e.g. Update every row so that 'xfirstword ' appears at the start of the text (e.g. Field1). Then you can search for CONTAINS(Field1, 'NEAR ((xfirstword, "TERM*"),0)')
Pretty crappy solution, especially as we know that the full text index stores the actual position of each word in the text (see this link for details: http://msdn.microsoft.com/en-us/library/ms142551.aspx)
I am facing the similar issue. This is what I have implemented as a work around.
I have made another table and pulled only the rows like 'some_term%'.
Now, on this new table I have implemented the FullText search.
Please do inform me if you tried some other better approach

Faceted Search in Coldfusion and SQL?

I'm working on a faceted search in Coldfusion and SQL. I've tried creating a query like this:
SELECT * FROM Turbos
WHERE
PartNumber LIKE '%#trim(SearchCriteria)#%'
OR PartDescription LIKE '%#trim(SearchCriteria)#%'
AND (PumpingSpeed BETWEEN #minimum# AND #URL.speed#)
AND InletFlange LIKE '#URL.inlet#'
AND Bearing LIKE '#URL.bearing#'
AND Condition LIKE '#URL.condition#'
The problem is the server is returning rows that don't contain EVERY piece of data I'm supplying. How can I select ONLY those rows which contain all the criteria?
just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
at the moment you have A or B and C which is being read as A or (B and C). You want (A or B) and C.
And make sure you use cfqueryparam as suggested above.
Just a guess really, but change
...'%#trim(SearchCriteria)#%' OR
PartDescription ...
to
...'%#trim(SearchCriteria)#%' AND
PartDescription ...
edit
or are you saying some have null values?
post comment edit
Imagine for example that InletFlange is empty for your part, and the user didn't put it in their search.
Then InletFlange LIKE '#URL.inlet#' will compare "" LIKE "", which is of course true, so the product shows up.
There are many ways to solve this. For example
Default the search criteria to "N/A" or something
Default the column in the database in a similar fashion.
something along the lines of AND NOT ISNULL(InletFlange,'')=='' for each criteria
Okay, I found the answers:
Leigh: +1 to clarifying the question. (It is
unlikely this has any bearing on the
results, but what is the point of
using LIKE without wildcards "%"? If
it is an equality comparison you are
after, just use equals ie ... AND
Condition = '#URL.condition#' )
You were quite right, I was missing the percent symbols in my LIKE comparisons (DUH!). I removed them earlier thinking they were the problem, but they weren't, this was:
Aiden: just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
Thank you Aiden!
While this isnt an answer to your question - you might want to consider using a dedicated search solution such as Solr (which is included with CF9). Its a more powerful fulltext solution than just raw SQL and includes facetting