2017-02-08 22 views
-1

這兩個Stuff語句有什麼不同嗎?我不是很正確的理解,因爲第一個塞滿'**',但是第二個連接列值。Stuff函數有什麼不同?

  1. select stuff(Name,2,3,'**')

  2. select stuff(','+Name from tablename for xml path(' ')),1,1,' ')

請讓我以簡單的方式清晰。

+0

你使用哪個dbms? (該功能是特定於產品的。) – jarlh

+0

我正在使用SQL服務器 –

+1

由於'xml path('')'不是因爲'STUFF'而導致第二個連接「 –

回答

2

STUFF函數:

STUFF函數將字符串插入另一個字符串。它會刪除開始位置第一個字符串中的指定長度的字符,然後將第二個字符串插入到開始位置的第一個字符串中。

例子:

select stuff('Stack overflow',1,5,'Knowledge') 

--output-- 

Knowledge overflow 

工具和XML路徑:

工具和XML是基於您的特定獨特的記錄用於連接多個記錄,並顯示在一個單一的錄音路徑

示例:

declare @t table(Id int, Name varchar(10)) 

insert into @t 
select 1,'a' union all 
select 1,'b' union all 
select 2,'c' union all 
select 2,'d' 

select ID, 
stuff(( select ','+ [Name] from @t where Id = t.Id for XML path('')),1,1,'') as concat_records 
from (select distinct ID from @t)t 

ID concat_records 
1 a,b 
2 c,d 

爲了更好understaning東西https://msdn.microsoft.com/en-us/library/ms188043.aspx

注:對不起,我的英語水平。希望你明白我想解釋。

0

select stuff('Name',2,3,'**') -- Start From 2nd Character and Replace 3 character with '**' 
 

 
select stuff(SELECT DISTINCT ', ' + Name 
 
          -- Add a comma (,) before each value 
 
from tablename FOR XML PATH('') -- Select it as XML 
 
         ), 1, 1, '') 
 
         -- This is done to remove the first character

相關問題