I have used a user-defined split function for splitting text into 3 pieces seperated by space character as follows
Of course if you have SQL Server 2016 or later then you can use STRING_SPLIT SQL function too
with rawdata as (
select rn = ROW_NUMBER() over (order by txt), * from drugs
), cte as (
select
rn,
d.txt,
s.id,
s.val
from rawdata d
cross apply dbo.Split(rtrim(ltrim(d.txt)),' ') s
)
select * from cte
Please note that the Row_Number rn column is required to identify each row in following script. If you have a PK field in your source table, instead of using rn field created by Row_Number function, you can directly use those Primary Key fields
And to split the second column (strength and units), I again preferred to use custom SQL functions; ClearNumericCharacters and ClearNonNumericCharacters
Of course you can use inline functions or RegExp instead of UDFs
Here is the final SQL CTE expression
with rawdata as (
select rn = ROW_NUMBER() over (order by txt), * from drugs
), cte as (
select
rn,
d.txt,
s.id,
s.val
from rawdata d
cross apply dbo.Split(rtrim(ltrim(d.txt)),' ') s
), cte2 as (
select
rn,
case when id = 1 then val end as Drugname,
case when id = 2 then dbo.ClearNonNumericCharacters(val) end as Strength,
case when id = 2 then dbo.ClearNumericCharacters(val) end as Units,
case when id = 3 then val end as Form
from cte
)
select
max(Drugname) Drugname,
max(Strength) Strength,
max(Units) Units,
max(Form) Form
from cte2
group by rn
And the output is
