| |

VerySource

 Forgot password?
 Register
Search
View: 608|Reply: 2

A question about string splitting. anxious

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-10 21:40:01
| Show all posts |Read mode
declare str varchar (100)
set str = 'aa, b, cd, de, ed'
I want to split str into commas and recycle all the values ​​and add them. What should I do?
Thank you. Don't tell me to just remove the comma, I mainly loop after opening.
Reply

Use magic Report

0

Threads

14

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-13 18:27:01
| Show all posts
create function f_split (@SourceSql varchar (8000), @StrSeprate varchar (10))
returns @temp table (a varchar (100))
/ *-A function that implements the split function
--date: 2005-4-20
--Author: Domino
* /
as
begin
    declare @i int
    set @ SourceSql = rtrim (ltrim (@SourceSql))
    set @ i = charindex (@ StrSeprate, @ SourceSql)
    while @i> = 1
    begin
        insert @temp values ​​(left (@ SourceSql, @ i-1))
        set @ SourceSql = substring (@ SourceSql, @ i + 1, len (@SourceSql)-@ i)
        set @ i = charindex (@ StrSeprate, @ SourceSql)
    end
    if @SourceSql <> '\'
       insert @temp values ​​(@SourceSql)
    return
end


select * from dbo.f_split ('Shandong: Jinan: Shandong: Jinan', ':')

select top 1 * from f_split ('Shandong: Jinan', ':')
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-1-14 10:18:01
| Show all posts
declare @str varchar (100)
set @ str = 'aa, b, cd, de, ed'
declare @sql varchar (8000)
select @ sql = 'select' ''
select @ sql = @ sql + replace (@str, ',', '' 'union all select' '') + '' ''
exec (@sql)

--result
----
aa
b
cd
de
ed
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list