A little ugly, but using a bit of JSON (to GTD the sequence) and the window function lead() over()
Example
Declare @YourTable table (ID int,SomeCol varchar(max))
Insert Into @YourTable values
(1,'host-001.servers.company.com desc=''Production Web Cache'' env=''Prod'' patch_round=''Beta'' dc=''Main'' rhel_v=''7.6'' primary=''[email protected]'' secondary=''[email protected]''')
Select A.ID
,Host = left(SomeCol,charindex(' ',SomeCol+' '))
,B.*
From @YourTable A
Cross Apply (
Select Item = ltrim(rtrim(right(Value,charindex(' ',reverse(Value)+' '))))
,Value = ltrim(rtrim(replace(
IsNull(lead( left(Value,nullif(len(Value)+1-charindex(' ',reverse(Value)+' '),0)),1) over (order by [Key])
,lead(right(Value,charindex(' ',reverse(Value)+' ')),1) over (order by [key])
),'''','')))
From OpenJSON( '["'+replace(string_escape(SomeCol,'json'),'=','","')+'"]' )
) B
Where B.Value is not null
Results
ID Host Item Value
1 host-001.servers.company.com desc Production Web Cache
1 host-001.servers.company.com env Prod
1 host-001.servers.company.com patch_round Beta
1 host-001.servers.company.com dc Main
1 host-001.servers.company.com rhel_v 7.6
1 host-001.servers.company.com primary [email protected]
1 host-001.servers.company.com secondary [email protected]
EDIT - Injected "HOST="
Declare @YourTable table (ID int,SomeCol varchar(max))
Insert Into @YourTable values
(1,'host-001.servers.company.com desc=''Production Web Cache'' env=''Prod'' patch_round=''Beta'' dc=''Main'' rhel_v=''7.6'' primary=''[email protected]'' secondary=''[email protected]''')
Select A.ID
,B.*
From @YourTable A
Cross Apply (
Select Item = ltrim(rtrim(right(Value,charindex(' ',reverse(Value)+' '))))
,Value = ltrim(rtrim(replace(
IsNull(lead(left(Value,nullif(len(Value)+1-charindex(' ',reverse(Value)+' '),0)),1) over (order by [Key])
,lead(right(Value,charindex(' ',reverse(Value)+' ')),1) over (order by [key])
),'''','')))
From OpenJSON( '["'+replace(string_escape('host='+SomeCol,'json'),'=','","')+'"]' )
) B
Where B.Value is not null
Results
ID Item Value
1 host host-001.servers.company.com
1 desc Production Web Cache
1 env Prod
1 patch_round Beta
1 dc Main
1 rhel_v 7.6
1 primary [email protected]
1 secondary [email protected]
=or'? If so, don't do this in SQL.String_Split()to see how viable it was... Revoking my suggestion and agreeing withLarnu. T-SQL and built-in functions is the wrong tool for the job.STRING_SPLIT), @JNevill , but it would need to run on a couple of assumptions: 1. A domain is present at the start and followed by a space. 2. Values cannot contain=. 3. All values are enclosed in single quotes (') and all names are not. It would, however, be truly ugly.