Can you explain the following SQL (postgres) code? - sql

I asked SO for a query to find all rows in a table with a 'code' entry that is a substring of the search string, with the added condition that it appears at the end of the search string.
So a query for '12345' should return '2345', '345', '45', and '5'.
I was given this answer, which works. I have read through the documentation but still don't understand the query. Can someone please explain
SELECT * from yourtable
where '12345' like '%' || Code

Normally a LIKE is used in the opposite way.
For example:
SELECT * FROM SomeTable
WHERE SomeColumn LIKE '%xxx%'
So you check if the column matches against a fixed string with a pattern.
But the clever thing about that answer was it did the opposite.
It checks a fixed string again a pattern that's created from a column.
SELECT * FROM SomeTable
WHERE 'bar456' LIKE '%' || SomeColumn;
In this example, if "SomeColumn" contains the value "56"?
Then '%' || SomeColumn forms the string '%56'
So 'bar456' is like '%56', since it ends with '56'
And 'bar456' is also like '%ar456'

There are two relevant documentation links you need:
PostgreSQL Pattern Matching: '12345' like '%'
PostgreSQL CONCATENATE(||) Operator: <match> || Code
The SQL means:
Fetch all columns from the table
IF column "code" is equal to <match> + <value of "code" column>

Related

How to perform Like in SQL on a column with '%' in the data?

I am using oracle database and writing the likeness filter for the persistence layer and want to perform likeness on a column that can possibly have '%' in it's data.
To filter data I am writing the query for likeness using LIKE clause as
select * from table where columnName like '%%%';
which is returning all the values but I only want the rows that contains '%' in the columnName.
Not sure what escape character to use or what to do to filter on the '%' symbol. Any suggestions??
Also, I have to do the same thing using Criteria api in java and have no clues about putting escape character there.
You can use an escape character.
where columnName like '%$%%' escape '$'
REGEXP_LIKE might help in a rather simple manner.
SQL> with test (col) as
2 (select 'abc%def' from dual union all
3 select '%12345&' from dual union all
4 select '%abc12%' from dual union all
5 select '1234567' from dual
6 )
7 select *
8 from test
9 where regexp_like(col, '%');
COL
-------
abc%def
%12345&
%abc12%
SQL>
If I have understood it correctly the answer from the #Littlefoot is correct(and I will up-vote it now). I will just add more details because you are looking for name of the columns of your table "I only want the rows that contains '%' in the columnName".
Here is the table I have created:
CREATE TABLE "table_WITH%" ("numer%o" number
, name varchar2(50)
, "pric%e" number
, coverage number
, activity_date date);
Then this query gives me the correct answer:
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'table_WITH%'
AND regexp_like(COLUMN_NAME, '%');
Gordon's answer using the escape clause of the like command is correct in the general case.
In your specific case (searching for a single character, anywhere in a string), a simpler method is to use instr, e.g.
where instr(columnname, '%') > 0

Compare two columns with 'LIKE' Operator in SQL / Laravel

I'm trying to compare two columns from a table. In which i have to check the email is containing his mobile number or not.
TableName:- Table1
TableColumns:- id,email,MOB
Ex.
SQL:
'SELECT * FROM Tabel1 WHERE email LIKE %MOB%'
Laravel :
Tabel1::whereColumn('email', 'LIKE','%MOB%')->get();
I have Tried this above query but it is showing syntax error.
Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:
SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');
Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:
Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");
or
Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));
You should try this
SELECT * FROM Tabel1 WHERE email LIKE '%' +MOB+'%'
SELECT * FROM `table` WHERE `email` LIKE '%MOB%';

SQL: finding similar rows to other same table in special column

I am using SQLITE for running this query:
SELECT * FROM phrases1, phrases2 WHERE phrases1.word LIKE ('%' +phrases2.word+ '%')
but not works.
two tables phrases1, phrases2 are same and have column name word and I want to filter the first table by rows that word column is similar to the word column of second table . while this works:
SELECT * FROM phrases1, phrases2 WHERE phrases1.word LIKE phrases2.word
but I want to use wildcards.
The SQLite operator for string concatenation is || not +:
SELECT * FROM phrases1, phrases2
WHERE phrases1.word LIKE '%' || phrases2.word || '%'
Also I don't know what the effect of having parentheses around your LIKE expression would be, but you don't need them there. But you should really write your query using explicit joins, better yet use aliases too:
SELECT *
FROM phrases1 p1
INNER JOIN phrases2 p2
ON p1.word LIKE '%' || p2.word || '%'

"NOT IN" subquery with a leading wildcard

I have two tables:
Table tablefoo contains a column fulldata.
Table tablebar contains a column partialdata.
I want find a list of tablefoo.fulldata that do NOT have partial matches in tablebar.partialdata.
The following provides a list of tablefoo.fulldata with partial matches in tablebar, but I want the negative of this.
select fulldata from tablefoo
where fulldata like any (select '%' || partialdata from tablebar);
This lists every record in partialdata:
select fulldata from tablefoow
where partialdata not in (select '%' || partialdata from tablebar);
Any idea how to get only the results tablefoo.fulldata that do not contain matches to a leading wildcarded tablebar.partialdata?
I found this link: PostgreSQL 'NOT IN' and subquery which seems like it's headed down the right path, but I'm not getting it to work with the wildcard.
Sure, I could write a script to pull this out of psql and do the comparisons, but it would be much nicer to handle this all as part of the query.
SELECT fulldata
FROM tablefoo f
WHERE NOT EXISTS (
SELECT 1
FROM tablebar b
WHERE f.fulldata LIKE ('%' || b.partialdata)
);

In a SQL query (DB2), how can I find a field value within another field?

In a SQL query (DB2), how can I find a field value within another field?
For example, Field01 contains part number '1234'. Field02 contains 'this is my 1234 to keep'
How can I find or search for whatever is the value of Field01 within Field02?
I've tried LOCATE and LIKE and % % and Position but cannot seem to get it.
I have been able to find Field01 in Field02 as long as it is in the beginning of Field02, but not if it is proceeded by other characters.
This is the command I've used for this:
CASE
WHEN LEFT ( TRIM ( FIELD02), CHARACTER_LENGTH ( TRIM ( FIELD01 ) ) ) =
TRIM ( WMCPIL ) THEN TRIM ( FIELD02)
ELSE '^'
END
The LOCATE function should work:
SELECT *
FROM table_name
WHERE LOCATE(field01,field02) <> 0
Your question states that you already tried LOCATE; if it did not work, please explain.
Here is a documentation reference.
You can use LIKE in following manner:
SELECT *
FROM mytable
WHERE field2 LIKE '%' || field1 || '%'
SQLFiddle demo (using PostgreSQL as SQLFiddle does not support DB2 yet, but this example should work on DB2 as well).
DB2 prefers to use CONCAT to concatenate strings, but it also supports standard || as a synonym.
It is working fine, try it-
SELECT * FROM TAB1 WHERE POSITION(field1,field2,CODEUNITS16)<>0