Home > database >  How SQL by semicolons intercept string
How SQL by semicolons intercept string

Time:02-26

The table name: TW
The field name: mrp_no_so
Value: C12; C13; C16

The table name: PRDT
The field name: prd_no, name
Value: C12, F36
C13, Y87
C16, G432

How to TW and PRDT two table join query to get the following value
F36; Y87; G432


CodePudding user response:

For the SQL SERVER database

CodePudding user response:

 

The CREATE TABLE # TW
(
Mrp_no_so VARCHAR (100)
)

INSERT INTO # TW VALUES (' C12; C13; C16 ')

The CREATE TABLE # PRDT
(
Prd_no VARCHAR (10),
The name VARCHAR (10)
)

INSERT INTO # PRDT VALUES (' C12 ', 'F36)
INSERT INTO # PRDT VALUES (' C13 ', 'Y87)
INSERT INTO # PRDT VALUES (' C16 ', 'G432')


SELECT *, STUFF ((SELECT '; '+ name FROM # PRDT WHERE CHARINDEX (prd_no mrp_no_so) & gt; Zero ORDER BY CHARINDEX (prd_no + '; ', mrp_no_so + '; ') FOR XML PATH (' ')), 1, 1, ' ') AS the results
The FROM # TW

DROP TABLE # PRDT
DROP TABLE # TW

  • Related