日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

SQL分割字符串 && SQL一列多行字符串分組合并

系統(tǒng) 2249 0

T-SQL對字符串的處理能力比較弱,比如我要循環(huán)遍歷象1,2,3,4,5這樣的字符串,如果用數(shù)組的話,遍歷很簡單,

但是T-SQL不支持?jǐn)?shù)組,所以處理下來比較麻煩。下邊的函數(shù),實(shí)現(xiàn)了象數(shù)組一樣去處理字符串。用臨時表作為數(shù)組:

ALTER function [dbo].[F_Limitsplit](@IDs varchar(max),@UserID int)
returns @t table(UserID int,ID int)
as
begin

while(charindex(',',@IDs)<>0)
begin
insert @t(UserID,ID) values (@UserID,substring(@IDs,1,charindex(',',@IDs)-1))
set @IDs = stuff(@IDs,1,charindex(',',@IDs),'')
end
insert @t(UserID,ID) values (@UserID,@IDs)
return
end

?

-----------執(zhí)行

?

select ? * ? from ? dbo.F_Limitsplit('1,2,3,4,5,6,7',1) ??

????
--------------------執(zhí)行結(jié)果

UserID?????ID

1     1
1     2
1     3
1     4
1     5
1     6
1     7

?

?

=====================================================

數(shù)據(jù)
id???data
1????a
1????b
1????c
2????aa
2????bb

結(jié)果:?
1?abc
2?aabb
就是要把data?列多行合并成一行顯示

要求:一句sql語句.?不能用sp.

-----------------------------

SQL?codecreate?table?tb(id?int,?data?varchar(10))
insert?into?tb?values(1?,???'a')?
insert?into?tb?values(1?,???'b')?
insert?into?tb?values(1?,???'c')?
insert?into?tb?values(2?,???'aa')?
insert?into?tb?values(2?,???'bb')?
go

select?id,?[data]=replace((select?','+[data]?from?tb?t?where?id=tb.id?for?xml?path('')),',','')
from?tb
group?by?id

drop?table?tb

--------------------------------------------

SELECT O.*,
ItemName=(select ProductName+',' from Bms_OrderGoodsDetail OGD
left join Bms_Products P on OGD.ProductID=P.ProductID
where OGD.OrderGoodsID= O.OrderGoodsID for xml path(''))
FROM [dbo].[Bms_OrderGoods] O

?

SQL分割字符串 && SQL一列多行字符串分組合并


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 通江县| 楚雄市| 阜平县| 南京市| 聂荣县| 西乌珠穆沁旗| 和田县| 繁昌县| 华安县| 铅山县| 奉贤区| 裕民县| 海南省| 沙洋县| 庆安县| 新昌县| 昆明市| 徐州市| 淅川县| 北辰区| 郧西县| 玛沁县| 灵台县| 漳州市| 苍山县| 色达县| 开化县| 明水县| 怀宁县| 周宁县| 吉水县| 平阳县| 荆门市| 当涂县| 射阳县| 乌兰浩特市| 天长市| 司法| 富平县| 广东省| 绍兴县|