新聞中心
這里有您想知道的互聯網營銷解決方案
同一列SQL字符串相加的方法
下面為您介紹的方法實現 同一列SQL字符串相加,供您參考學習,如果您也遇到過類似的問題,不妨一看,相信對您會有所啟迪。

天臺網站制作公司哪家好,找創(chuàng)新互聯!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、成都響應式網站建設公司等網站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯成立于2013年到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創(chuàng)新互聯。
- Create table Tab([Col1] int,[Col2] nvarchar(1))
- Insert Tab
- select 1,N'a' union all
- select 1,N'b' union all
- select 1,N'c' union all
- select 2,N'd' union all
- select 2,N'e' union all
- select 3,N'f'
- Go
- create function F_Str(@Col1 int)
- returns nvarchar(100)
- as
- begin
- declare
- @S nvarchar(100)
- select @S=isnull(@S,'')+Col2 from Tab where Col1=@Col1
- return @S
- end
- go
- Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
- go
- ================================
說明:
目標:表的一列為SQL字符串,要求把該列SQL字符串連起來。
如:
1,aa,
1,bb
2,cc
2,dd
結果:
1,aabb
2,ccdd
如果要出現aa,bb cc,dd的結果
那isnull(@S,'')改成isnull(@S+',','')
isnull(@S,''): 如果@S是空,則用''代替,因為null+字符串 = null
【編輯推薦】
按時間點還原SQL數據庫的方法
教您如何修改sql server時間
Sql Server日期與時間函數簡介
SQL Server時間算法大全
帶您學習SQL存儲過程
名稱欄目:同一列SQL字符串相加的方法
轉載源于:http://www.5511xx.com/article/dhhedcc.html


咨詢
建站咨詢
