SQL: Select a column where a longer string is like a column - sql

I want to change some data in the database where the text of a column is in a string.
For example we have the String: 'QWZ-$ §_REMARKIUFZQ'
Now I want to select a column where the column name has the text 'Mark'.
Something like this stupid try:
SELECT info FROM database WHERE '%'+name+'%' LIKE 'QWZ-$ §_REMARKIUFZQ'
Best Regards,
Christian

From sqlite doc:
The operand to the right of the LIKE operator contains the pattern and the left hand operand contains the string to match against the pattern.
Switch the LIKE operands i.e. WHERE 'QWZ-$ §_REMARKIUFZQ' LIKE '%'||name||'%' (notice + changed to sqlite concatenate operator || for native sqlite syntax).
Assuming sqlite3 version >= 3.16, you may find the table valued function pragma_table_info is the place to look.

Related

SQL LIKE - Using square bracket (character range) matching to match an entire word

In REGEX you can do something like [a-c]+, which will match on
aaabbbccc
abcccaabc
cbccaa
b
aaaaaaaaa
In SQL LIKE it seems that one can either do the equivalent of ".*" which is "%", or [a-c]. Is it possible to use the +(at least one) quantifier in SQL to do [a-c]+?
EDIT: Just to clarify, the desired end-query would look something like
SELECT * FROM table WHERE column LIKE '[a-c]+'
which would then match on the list above, but would NOT match on e.g "xxxxxaxxxx"
As a general rule, SQL Server's LIKE patterns are much weaker than regular expressions. For your particular example, you can do:
where col not like '%[^a-c]%'
That is, the column contains no characters that are not a, b, or c.
You can use regex in SQL with combination of LIKE e.g :
SELECT * FROM Table WHERE Field LIKE '%[^a-z0-9 .]%'
This works in SQL
Or in your case
SELECT * FROM Table WHERE Field LIKE '%[^a-c]%'
I seems you want some data from database, That is you don't know exactly, You must show your column and the all character that you want in that filed.

SQL String contains ONLY

I have a table with a field that denotes whether the data in that row is valid or not. This field contains a string of undetermined length. I need a query that will only pull out rows where all the characters in this field are N. Some possible examples of this field.
NNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNN
NNNNNEEEENNNNNNNNNNNN
NNNNNOOOOOEEEENNNNNNNNNNNN
Any suggestions on a postcard please.
Many thanks
This should do the trick:
SELECT Field
FROM YourTable
WHERE Field NOT LIKE '%[^N]%' AND Field <> ''
What it's doing is a wildcard search, broken down:
The LIKE will find records where the field contains characters other than N in the field. So, we apply a NOT to that as we're only interested in records that do not contain characters other than N. Plus a condition to filter out blank values.
SELECT *
FROM mytable
WHERE field NOT LIKE '%[^N]%'
I don't know which SQL dialect you are using. For example Oracle has several functions you may use. With oracle you could use condition like :
WHERE LTRIM(field, 'N') = ''
The idea is to trim out all N's and see if the result is empty string. If you don't have LTRIM, check if you have some kind of TRANSLATE or REPLACE function to do the same thing.
Another way to do it could be to pick length of your field and then construct comparator value by padding empty string with N. Perhaps something like:
WHERE field = RPAD('', field, 'N)
Oracle pads that empty string with N's and picks number of pad characters from length of the second argument. Perhaps this works too:
WHERE field = RPAD('', LENGTH(field), 'N)
I haven't tested those, but hopefully that give you some ideas how to solve your problem. I guess that many of these solutions have bad performance if you have lot of rows and you don't have other WHERE conditions to select proper index.

SQLite string contains other string query

How do I do this?
For example, if my column is "cats,dogs,birds" and I want to get any rows where column contains cats?
Using LIKE:
SELECT *
FROM TABLE
WHERE column LIKE '%cats%' --case-insensitive
While LIKE is suitable for this case, a more general purpose solution is to use instr, which doesn't require characters in the search string to be escaped. Note: instr is available starting from Sqlite 3.7.15.
SELECT *
FROM TABLE
WHERE instr(column, 'cats') > 0;
Also, keep in mind that LIKE is case-insensitive, whereas instr is case-sensitive.

db2 special character 0x1A

How can I seach for a special character like "→" (0x1A)?
An example for my query is:
select * from Data where Name like '%→%'
I want to use instead of "→" something like 0x1A.
I can't use any Java or C# Code. I just have SQuirrel to connect and send commands to the database.
You can use chr() to search the charater:
select * from data where name like '%' + chr(26) + '%'
Old topic, but a current issue nevertheless. My DB2 environment runs on AS400/iSeries and use EBCDIC instead of ASCII. I worked out the character from the EBCDIC table at lookuptables.com.
ASCII 0x1a translates to SUB, and SUB translates to EBCDIC 0x3f.
Using that in SQL looks like this:
select productDescription
from productsTable
where productDescription like '%' || x'3F' || '%'`
Solution 1: Use chr(26).
Solution 2: Write the query in Java or another programming language which lets you build the SQL from pieces and where you can enter hex codes.
This query has worked for me in the past on iSeries DB2.
select * from db/table where posstr(field, x'3F') > 0
Trouble is you have to be certain of the hex value you are searching for in the string. I had a similar situation where the I was sure the hex code for the character was x'3F, but when I sub-string the non-viewable character it was actually x'22. You might want to single out the character that is giving you the issue and see what it's value is.
select hex(substr(field, 21,1)) from db/table where posstr(field, 'StringBeforeCharacter') > 0

Is it possible to get the matching string from an SQL query?

If I have a query to return all matching entries in a DB that have "news" in the searchable column (i.e. SELECT * FROM table WHERE column LIKE %news%), and one particular row has an entry starting with "In recent World news, Somalia was invaded by ...", can I return a specific "chunk" of an SQL entry? Kind of like a teaser, if you will.
select substring(column,
CHARINDEX ('news',lower(column))-10,
20)
FROM table
WHERE column LIKE %news%
basically substring the column starting 10 characters before where the word 'news' is and continuing for 20.
Edit: You'll need to make sure that 'news' isn't in the first 10 characters and adjust the start position accordingly.
You can use substring function in a SELECT part. Something like:
SELECT SUBSTRING(column, 1,20) FROM table WHERE column LIKE %news%
This will return the first 20 characters from column column
I had the same problem, I ended up loading the whole field into C#, then re-searched the text for the search string, then selected x characters either side.
This will work fine for LIKE, but not full text queries which use FORMS OF INFLECTION because that may match "women" when you search for "woman".
If you are using MSSQL you can perform all kinds VB-like of substring functions as part of your query.