reza ramezani matin
reza ramezani matin

Reputation: 1474

how to get output by part of data in another table

Table A:
id      code
---------------
1        90
2        85
3        75

Table B:

id            title
---------------------
902313        reza
854646        matin
755346        amir
602545        martin
105456        ghdsa

value in Code filed of table A is part of id of Table B. i want to get output from Table B(following output) where Table A have part of value of id of table B

----output-----
id            title
---------------------
902313        reza
854646        matin
755346        amir

another word how to get query on Table B where id of Table B is part of Code of Table A.

Upvotes: 2

Views: 58

Answers (1)

Lalit Kumar B
Lalit Kumar B

Reputation: 49062

You need to join using SUBSTR function.

The substring would give you the following output:

SQL> SELECT substr(902313, 1, 2) FROM dual;

SU
--
90

SQL>

Required query:

SELECT b.* 
  FROM table_a a, table_b b
 WHERE TO_CHAR(a.id) = SUBSTR(b.code, 1, 2);

Use TO_CHAR on id column if it is a NUMBER type to avoid any implicit data type conversion.

Upvotes: 1

Related Questions