0

Simple string splitting for insertion

1
2
3
4
5
6
7
8
DECLARE @string varchar(50), @execSql varchar(1000)
SET @string = 'I like kittens'
 
SET @execSql = 'INSERT INTO table (column) SELECT '''
SET @execSql = @execSql + REPLACE(@string, ' ', ''' UNION ALL SELECT ''')
SET @execSql = @execSql + ''''
 
EXEC (@execSql)

The snippet above will prepare a statement for insertion and replace the space in the string to be split with UNION ALL SELECT.

INSERT INTO table (column) SELECT 'I' UNION ALL SELECT 'like' UNION ALL SELECT 'kittens'

Source

Norbert Krupa

Technical Consultant

Leave a Reply

Your email address will not be published. Required fields are marked *