SQL Split Row Data Separated by Spaces
1
vote
2
answers
4924
views
I am looking for a query
to find nth value in a list. The separator is anything greater than or equal to 2 spaces. (it can be 3, or 5 spaces).
Trying to avoid scalar value functions, since performance may be slower. The sentences can have any number of words, from 5-20.
CREATE TABLE dbo.TestWrite (TestWriteId int primary key identity(1,1),
TextRow varchar(255))
INSERT INTO dbo.TestWrite (TextRow)
SELECT 'I am writing SQL Code.'
UNION ALL
SELECT 'SQL keywords include join, except, where.'
+-----+----------+---------+---------------+---------+----------+
| SQL | keywords | include | join, | except, | where. |
+-----+----------+---------+---------------+---------+----------+
| I | am | writing | SQL Code. | | |
+-----+----------+---------+---------------+---------+----------+
Would like in individual rows with columns, see comments above.
This may be one solution trying to utilize.
https://stackoverflow.com/questions/19449492/using-t-sql-return-nth-delimited-element-from-a-string
DECLARE @dlmt NVARCHAR(10)=N' ';
DECLARE @pos INT = 2;
SELECT CAST(N'' + REPLACE(@input,@dlmt,N'') + N'' AS XML).value('/x[sql:variable("@pos")]','nvarchar(max)')
Asked by user172334
Feb 28, 2019, 11:28 PM
Last activity: Apr 13, 2020, 05:54 AM
Last activity: Apr 13, 2020, 05:54 AM