2017-01-13 43 views
0

之間分離字符`「`的字符串的多個部分我得到串兩個表現在,這些字符串包括由一個"分隔不同部分的,例如:Comparings與兩個表

表1:

--------------------- 
| 1 | hello"world | 
--------------------- 
| 2 | hows"life"bro | 
--------------------- 

表2:

----------------------- 
| 1 | world"hello  | 
----------------------- 
| 2 | hows"bro"life | 
----------------------- 

在我得到了這樣的查詢的時刻:

select * from table1 inner join table2 on table1.id = table2.id where table1.colum2 = table2.colum2

我需要應顯示兩行,因爲如果你改變了helloworld從表1你world"hello該表2中確實含有查詢。我怎樣才能做到這一點?

在此先感謝

+0

它總是用'''分隔嗎? –

+0

@Prdp是它總是用''' –

回答

1

您需要拆分字符串函數來做到這一點

;WITH tab1 
    AS (SELECT * 
     FROM (VALUES (1,'hello"world'), 
         (2,'hows"life"bro')) t1 (id, NAME) 
       CROSS apply (SELECT * 
          FROM Delimitedsplit8k(t1.NAME, '"')) cs), 
    tab2 
    AS (SELECT * 
     FROM (VALUES (1,'world"hello'), 
         (2,'hows"bro"life')) t2 (id, NAME) 
       CROSS apply (SELECT * 
          FROM Delimitedsplit8k(t2.NAME, '"')) cs) 
SELECT DISTINCT t1.id, 
       t1.NAME, 
       t2.id, 
       t2.NAME 
FROM tab1 t1 
     JOIN tab2 t2 
     ON t1.Item = t2.Item 

從這裏

Tally OH! An Improved SQL 8K 「CSV Splitter」 Function

0

創建分割字符串可以轉換column2xml和使用xquery對值進行排序並進行比較。

declare @T1 table (id int, col varchar(max)) 
insert into @T1 (id, col) values (1, 'hellow"world'), (2, 'a"b"c') 

declare @T2 table (id int, col varchar(max)) 
insert into @T2 (id, col) values (1, 'world"hellow'), (2, 'b"c"a') 

select * 
from @T1 as t1 
inner join @T2 as t2 on t1.id = t2.id 
where 
    cast('<a>' + replace(t1.col, '"', '</a><a>') + '</a>' as xml).query('for $x in /a order by $x return string($x)').value('/', 'varchar(max)') = 
    cast('<a>' + replace(t2.col, '"', '</a><a>') + '</a>' as xml).query('for $x in /a order by $x return string($x)').value('/', 'varchar(max)')