Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SQL merge strings by a field

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article editor for you detailed introduction of "SQL how to merge strings according to a field", the content is detailed, the steps are clear, the details are handled properly, I hope this "SQL how to merge strings according to a field" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Title: merge one of the strings by a field (simple merge)

Description: merge the following form of data into the value field by id field. Id value--1 aa1 bb2 aaa2 bbb2 ccc needs to get the result: id value--1 aa,bb2 aaa,bbb,ccc is: group by id, find the sum of value (string addition)

1. Only custom functions can be used in sql2000.

Create table tb (id int, value varchar (10)) insert into tb values (1, 'aa') insert into tb values (1,' bb') insert into tb values (2 aaa') insert into tb values (2 bbb') insert into tb values (2 insert into tb values CCC') gocreate function dbo.f_str (@ id int) returns varchar (100) asbegin declare @ str varchar (1000) set @ str='' select @ str=@str+''+cast (value as varchar) from tb where id = @ id set @ str=right (@ str) Len (@ str)-1) return @ strendgo-- calls the function select id, value = dbo.f_str (id) from tb group by iddrop function dbo.f_strdrop table tb

2. Methods in sql2005

Create table tb (id int, value varchar (10)) insert into tb values (1, 'aa') insert into tb values (1,' bb') insert into tb values (2, 'aaa') insert into tb values (2,' bbb') insert into tb values (2, 'ccc') goselect id, [value] = stuff ((select','+ [value] from tb t where id = tb.id for xml path (')), 1, 1,') from tb group by iddrop table tb

3. Use cursors to merge data

Create table tb (id int,value varchar (10)) insert into tb values (1, 'aa') insert into tb values (1,' bb') insert into tb values (2, 'aaa') insert into tb values (2,' bbb') insert into tb values (2, 'ccc') godeclare @ t table (id int,value varchar (100))-define result set table variables-define cursors and merge declare my_cursor cursor local forselect id, value from tbdeclare @ id_old int @ id int, @ value varchar (10), @ s varchar (100) open my_cursor fetch my_cursor into @ id, @ valueselect @ id_old = @ id, @ s=''while @ @ FETCH_STATUS = 0begin if @ id = @ id_old select @ s +','+ cast (@ value as varchar) else begin insert @ t values (@ id_old, stuff (@ smeary)) select @ swatch','+ cast (@ value as varchar) @ id_old = @ id end fetch my_cursor into @ id, @ value END insert @ t values (@ id_old, stuff) close my_cursor deallocate my_cursor select * from @ tdrop table tb This article "SQL how to merge strings by a certain field" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report