久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否可以在不循環(huán)的情況下比較 T-SQL 中的逗號(hào)分

Is it possible to compare comma delimited string in T-SQL without looping?(是否可以在不循環(huán)的情況下比較 T-SQL 中的逗號(hào)分隔字符串?)
本文介紹了是否可以在不循環(huán)的情況下比較 T-SQL 中的逗號(hào)分隔字符串?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

假設(shè)我有 2 個(gè)表,其中都有名為 Brand 的列.該值以逗號(hào)分隔,例如,如果表之一具有

Let's say I have 2 tables where both has column called Brand. The value is comma delimited so for example if one of the table has

ACER,ASUS,HP  
AMD,NVIDIA,SONY

作為價(jià)值.然后另一個(gè)表有

as value. Then the other table has

HP,GIGABYTE  
MICROSOFT  
SAMSUNG,PHILIPS

作為值.

我想比較這些表以獲取所有匹配的記錄,在我的示例中 ACER,ASUS,HPHP,GIGABYTE 匹配,因?yàn)閮烧叨加?HP.現(xiàn)在我正在使用循環(huán)來實(shí)現(xiàn)這一點(diǎn),我想知道是否可以在單個(gè)查詢語法中做到這一點(diǎn).

I want to compare these table to get all matched record, in my example ACER,ASUS,HP and HP,GIGABYTE match because both has HP. Right now I'm using loop to achieve this, I'm wondering if it's possible to do this in a single query syntax.

推薦答案

您想擺脫循環(huán)是正確的.

You are correct in wanting to step away from the loop.

自從您進(jìn)入 2012 年以來,String_Split() 不在討論范圍內(nèi).然而,有許多分裂/解析 TVF 函數(shù)在野中.

Since you are on 2012, String_Split() is off the table. However, there are any number of split/parse TVF functions in-the-wild.

示例 1 - 沒有 TVF

Declare @T1 table (Brand varchar(50))
Insert Into @T1 values 
('ACER,ASUS,HP'),
('AMD,NVIDIA,SONY')

Declare @T2 table (Brand varchar(50))
Insert Into @T2 values 
('HP,GIGABYTE'),
('MICROSOFT'),
('SAMSUNG,PHILIPS')


Select Distinct
       T1_Brand = A.Brand
      ,T2_Brand = B.Brand
 From ( 
        Select Brand,B.*
         From  @T1
         Cross Apply (
                        Select RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace(Brand,',','</x><x>')+'</x>' as xml)) as A 
                        Cross Apply x.nodes('x') AS B(i)
                     ) B
      ) A
 Join ( 
        Select Brand,B.*
         From  @T2
         Cross Apply (
                        Select RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace(Brand,',','</x><x>')+'</x>' as xml)) as A 
                        Cross Apply x.nodes('x') AS B(i)
                     ) B
      ) B
 on A.RetVal=B.RetVal

示例 2 - 使用 TVF

Select Distinct
       T1_Brand = A.Brand
      ,T2_Brand = B.Brand
 From ( 
        Select Brand,B.*
         From  @T1
         Cross Apply [dbo].[tvf-Str-Parse](Brand,',') B
      ) A
 Join ( 
        Select Brand,B.*
         From  @T2
         Cross Apply [dbo].[tvf-Str-Parse](Brand,',') B
      ) B
 on A.RetVal=B.RetVal

兩人都會(huì)回來

T1_Brand        T2_Brand
ACER,ASUS,HP    HP,GIGABYTE

感興趣的 UDF

CREATE FUNCTION [dbo].[tvf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table 
As
Return (  
    Select RetSeq = Row_Number() over (Order By (Select null))
          ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
    From  (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
    Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[tvf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[tvf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[tvf-Str-Parse]('this,is,<test>,for,< & >',',')

這篇關(guān)于是否可以在不循環(huán)的情況下比較 T-SQL 中的逗號(hào)分隔字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Sql server table usage statistics(Sql server 表使用情況統(tǒng)計(jì))
Relative path in t sql?(t sql中的相對路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數(shù)據(jù)的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數(shù)中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時(shí)表性能)
主站蜘蛛池模板: 亚洲精品美女视频 | 亚洲精品天堂 | 日韩久久久久久 | 久久精品亚洲国产奇米99 | 欧美日韩高清免费 | 国产一区二区在线免费观看 | 精品久久久久久久 | 国产成人精品一区 | 亚洲精品日本 | 特级毛片| 欧美在线观看一区二区 | 一区二区在线免费观看视频 | 国产精品极品美女在线观看免费 | 亚洲综合一区二区三区 | 伊人色综合久久久天天蜜桃 | 欧美黄色小视频 | 久久国产高清 | 日本欧美在线视频 | 亚洲欧美激情精品一区二区 | 久久精品色欧美aⅴ一区二区 | 在线不卡视频 | 天天综合久久网 | 自拍 亚洲 欧美 老师 丝袜 | 在线一区视频 | 国产a级黄色录像 | 羞羞视频免费观 | 久久亚洲一区二区三区四区 | av在线一区二区三区 | 成人欧美一区二区三区在线播放 | 中文字幕欧美日韩 | 午夜影院在线观看 | 久久99精品久久久97夜夜嗨 | 久久精品综合网 | 亚洲精品久久久一区二区三区 | 国产精品一区三区 | 日韩高清电影 | 福利片在线 | 成人久久久久 | 日韩一区二区三区在线观看 | 亚洲综合在线一区 | 综合久久综合久久 |