I've executed the query on DB2:
SELECT * FROM MYTABLE WHERE MYFIELD LIKE '%B'
Although I know there are records that end with 'B' in the database, the query returned no results. After some research, it seems that DB2 is not recognizing LIKE expressions that don't end with '%'. So the following query would work:
SELECT * FROM MYTABLE WHERE MYFIELD LIKE '%B%'
but naturally not as expected, because it will return only the rows, where MYFIELD contains 'B', but doesn't end with it.
How to go around that strage, hmmm, feature? How to match the text on the end of the word in LIKE-like expressions?
DB2 can match patterns at the end of the string. The problem is probably that there are other characters.
You can try:
WHERE rtrim(MYFIELD) LIKE '%B'
You can also look at the lengths of the field and delimit the string value to see if there are other characters:
select length(MyField), '|' || MyField || '|'
from mytable
where MyField like '%B%';
If 'like' operator does not work for you, you can try regular expressions (with xQuery)
Related
I am attempting to generate a new field based on an existing field where some of the entries contain different special characters. (ie. *, ') The special characters are at the end of the string, which is either second or third position.
I am NEW to SQL but not new to data. I am using a CASE WHEN statement. I have tried several approaches and several other commands within the CASE statement.
what I want is:
SELECT *
CASE WHEN grde_code_mid LIKE '[*]' THEN 'Remedial'
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble
I keep getting the same error: "FROM keyword not found where expected". I expect to have all 3 returns in the new field.
If you're looking for ' character you should escape it.
Change
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
by
WHEN grde_code_mid LIKE '['']' THEN 'Continuing'
Have a look at this question: How do I escape a single quote in SQL Server?
There are several issues with your query:
you are missing a comma in the SELECT clause between * and the CASE expression (this is causing the error that you are currently getting)
the bracket notations is only supported in SQL-Server; if you want to match on strings that end with * in a portable manner, you need expression ... LIKE '%*', not LIKE '[*]'
single quotes embedded in a string need to be escaped
SELECT *, other_field FROM ... is not supported on all RDBMS (and, as commented by jarhl, actually isn't standard ANSI SQL notation); you usually need to prefix the * with the table name or alias
Consider:
SELECT
g.*,
CASE
WHEN grde_code_mid LIKE '%*' THEN 'Remedial'
WHEN grde_code_mid LIKE '%''' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble g
Thank you all. I am still getting the hang of the language and proper terms. I am using Oracle DB. Also, yes, I did need to reference the table in the select statement (SELECT tablename.*). Found a work around:
CASE WHEN regexp_like(grde_code_mid, '[*]') THEN 'Remedial'
WHEN regexp_like(grde_code_mid, '['']') THEN 'Continuing'
ELSE NULL END AS special_class
I am doing a
select field1
from tablename
where field1 like '%xyz zrt bla bla trew%'
field1 is a clob column and between the 'xyz zrt bla bla trew' there might be new line characters like chr(10) or chr(13). So it might be 'xyz\r\n\rzrt bla bla\n\n trew' etc
These are being converted to one (or more spaces). So any spaces between words can be true spaces or one or more of those new line characters.
How do I take this into account in my LIKE?
I am using Oracle but if possible I would like to use something that works for SQL Server, etc.
The lazy solution (relying on regular expressions, which may or may not kill performance - which may or may not matter) would be something like this:
select field1
from tablename
where regexp_like(field1, 'xyz\s+zrt\s+bla\s+bla\s+trew')
For Oracle you can use INSTR(field1, chr(10)) > 0
For SQL Server you can use field1 LIKE '%' + CHAR(10) + '%'
or CHARINDEX(CHAR(10), field1) > 0
You can replace the \n and \r with spaces first then trim the spaces and then compare. That would always work and perhaps fast enough.
However for proper speeding up the search process you can store the trimming results in a designated column "field1_trim".
Depends on your needs - storing the trimming results into a temporary table might be enough and more balanced between space/speed solution.
For example: save the result of the following query into a temporary table and then run your query on it
select
regexp_replace('[[:space:]]+', chr(32))
field1_trim,
<some unique row id to map to original table>
from table1;
I am after all cells containing the 'LINE SEPARATOR' (U+2028) unicode point. Normally this is encoded as \u+2028 or something similar. However googling how this translates to SQL has given various options none of which seem to work ((N'2028'), set #hexstring = '2028';, vchar(2028))
SELECT * FROM myTable WHERE desc LIKE '% [SOME WAY TO ESCAPE U+2028 ] %'
ANSI SQL answer, may or may not work with Postgresql.
SELECT * FROM myTable WHERE desc LIKE U&'%\2028%'
Alternative solution using regexp:
select * from mytable where desc ~ '\x2028';
I'm trying to find clobs in an oracle database that contain specific unicode whitespace characters, but I'm not sure how to the select statement can be written.
Trying the following query, but not sure if this is the correct way to go about it:
select * from mytable where my_clob like '%'|| n'0xEF' || '%';
I'm not sure what character you're wanting to search for, but I think it's the UNISTR command you're looking for. Also, LIKE will do an implicit conversion to VARCHAR2 (I think), so you're only searching in the first 4000 characters. Try this to search for the non-breaking space:
SELECT *
FROM mytable
WHERE dbms_lob.instr( my_clob, UNISTR( '\00A0' )) > 0;
https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions224.htm#SQLRF06154
If you know the character code of the character you're looking for, you can use CHR() to build an expression to look for the specific code;
SELECT *
FROM mytable
WHERE my_clob LIKE '%' || CHR(15052183) || '%';
An SQLfiddle to test with.
I would like to select all records that have an underscore character in their 11th character,
so i try this:
SELECT * FROM "BOM_SUB_LEVEL" where TOP_CODE like '%%%%%%%%%%_%%%'
but this doesnt work as expected, can someone help?
Just use the "SUBSTRING" function :
SELECT * FROM "BOM_SUB_LEVEL" where SUBSTRING(TOP_CODE, 11, 1) = "_"
Marc
For a single character wildcard use _. For multiple characters wildcards, use %. To escape a "real" appearance of _, use \_ (thanks Bill!).
Try the following code:
SELECT * FROM "BOM_SUB_LEVEL" where TOP_CODE like '___________\_%'
To further elaborate following Dav's comment, note that '%%%' is exactly the same as '%', since by definition '%' covers multiple characters.
pervasive uses _ to match any single character and \_ to actually match an underscore.
so the select would be:
SELECT * FROM "BOM_SUB_LEVEL" where TOP_CODE like '___________\_%'
LIKE % can mean any number of characters, use LIKE _ to mean just one. Since you're looking for an underscore, you need to escape it with !.
SELECT * FROM BOM_SUB_LEVEL WHERE TOP_CODE LIKE '__________!_%'
The % is not a per character wildcard, its a beginning and end of string wild card.
i.e. if I want to find all rows that have "car" in them, I would do this:
Select * from myTable where myCol LIKE '%car%'
If I wanted just the rows that STARTED with car:
Select * from myTable where myCol LIKE 'car%'
and ended with car:
Select * from myTable where myCol LIKE '%car'
% is a wildcard and can replace an character, or combination of characters. Use ? instead which replaces a single character.
You can try something like: (play with the numbers, I don't have pervasive to test with)
SELECT *
FROM BOM_SUB_LEVEL
where SUBSTRING(TOP_CODE, 11,1) = '-'