SQL condition - starts with a dynamic value - sql

I am writing a DB2 Stored procedure where I need to have a condition that select all values from table 1 and table 2 where table1.column_a starts with table2.column_b.
select * from table1 T1, table2 T2 where T1.column_a like T2.column_b + '%'
I tried playing around the above SQL but it seems like an invalid SQL. Any suggestions?

You can use the concat() function or || concatenation operator. + for string concatenation is used by SQL Server and similar databases.
I would phrase this as a join:
select *
from table1 T1 join
table2 T2
on T1.column_a like concat(T2.column_b, '%');

Consider that column_a and column_b has trailing blanks and that there is an issue with like operand. You could always use left() and combine it with RTRIM(). I've have very limited experience with DB2 but this is my hunch.
select *
from table1 T1
INNER JOIN table2 T2
on LEFT(RTRIM(T1.column_a), LENGTH(RTRIM(T2.column_b))) = T2.column_b

Related

How to use substring function in sql to extract when length of value is unknown

select * from table1 t1 join table2 t2 on
substring('t1.column1',9,x)=t2.column2;
This is the SQL query. i want to do this join on value of column1 after first 9 characters. But the value of column1 is not always consistent in length. What should be the value of 'x' here?
One method would be like:
select *
from table1 t1 join
table2 t2
on t2.column2 like '_________' || t1.column1;
-------------------------^ exactly 9 underscores
Note: This idea should work in any database. However, not all databases support the SQL Standard || string concatenation operator, so you might have to adjust the syntax.

How to select data where value in a comma split column when using hive sql

Let's say I have 2 tables: t1 and t2, the data looks like below:
Both colA and colB are stored as string.As you can see, the data in colB is splited by comma.
Now I want to make a join between 2 tables and the condition is that if colA's value can match colB's value after splited by comma.
I imagine the code may looks like:
select * from t1 join t2 on t1.colA in split(t2.colB,',')
and the result should look like:
I wonder how can I achieve the same effect in Hive sql. I hope my statement is clear. Please help if you have any idea, thanks!
You should fix the data model. Storing multiple values in a string is just not the SQL way to store data. The values should be in separate rows.
If you are stuck with someone else's really, really bad data model, you can use it, but the query is much less efficient than it otherwise would be:
select *
from t1 join
t2
on ',' || t2.colB || ',' like '%,' || t1.colA || ',%';
You can try array_contains:
select *
from t1
join t2
on array_contains(split(t2.colB,','), t1.colA)
select *
from t1
join (select t2.*
from t2 lateral view outer explode(split(t2.colB,',')) e as colB_exploded
) t2
on t2.colB_exploded=t1.colA

Joining multiple tables by removing leading alpha character

I need to match column.table1 = '1234' to column.table2 = 'B1234'. Is there a way to bypass the leading alpha character in table2? More specifically, can this be achieved by using an INNER JOIN clause?
You can join them via a LIKE.
An underscore _ can match any-1 character in the LIKE syntax.
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.col LIKE CONCAT('_', t1.col)
In SQL Server, you could do something like this:
SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON CAST(t1.column AS VARCHAR) = SUBSTRING(t2.column,2,LEN(t2.column)-1)
The SUBSTRING() function here takes the value of Table2.column starting at the second position, thus skipping the alpha character at the beginning.

Join tables on a partially matched columns

How to join two tables on a partially matched columns in SQL?
For example:
T1.column has the string 'c/ar' and T2.column has the string 'ar' and I want the tables to be joined in this case.
I tried
select column2,
column3
from T1 join T2 on T1.column like '%T2.column';
but it returns 0 rows.
Alternatively, you may use strpos function as
select column1, column2
from T1
join T2 on strpos(T1.column1,T2.column2)>0;
Rextester Demo
The PostgreSQL strpos() function is used to find the position, from where the substring is being matched within the string.
Syntax:
strpos( < string > , < substring >)
You can also use RIGHT() and LENGTH() functions since you are checking if T2.Column exists in the end of T1.Column
ON RIGHT(T1.Column1, LENGTH(T2.Column2)) = T2.Column2
Demo
You need to concatenate '%' with column T2.column and not with the string value 'T2.column':
like ('%' || T2.column)
or
like CONCAT('%', T2.column)

Use a Select Statement within a wildcard where clause

Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:
SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'
I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?
You can do a join and construct the like string based on table_2.
SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'