SQL split string and modify data in the database table

Source: Internet
Author: User
SELECT * FROM dbo. aabbUPDATE dbo. aabb SET bb = dbo. fun_GetName (aaa, 1), cc = dbo. fun_GetName (aaa, 2), dd = dbo. fun_GetName (aaa, 3) -- string to be split by the first parameter -- number of splits by the second parameter, such as 'aa, bb, the bb in the cc 'string is the 2nd create function fun_GetName (@ Name VARCHAR (200), @ pos int) returns varchar (50) to be split) -- returns a string as begin declare @ I int DECLARE @ ret VARCHAR (50) SET @ I = 0 WHILE (CHARINDEX (',', @ Name, 1)> 0) begin set @ I = @ I + 1 IF (@ I = @ pos) SET @ ret = LEFT (@ name, CHARINDEX (',', @ Name, 1)-1) SET @ name = RIGHT (@ name, LEN (@ name)-CHARINDEX (',', @ Name, 1) end return @ retend

 

No data before modification

Modified Data Format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.