Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need is a split user-defined function. With that, the solution looks like</p> <pre><code>With SplitValues As ( Select T.Name, Z.Position, Z.Value , Row_Number() Over ( Partition By T.Name Order By Z.Position ) As Num From Table As T Cross Apply dbo.udf_Split( T.Name, ' ' ) As Z ) Select Name , FirstName.Value , Case When ThirdName Is Null Then SecondName Else ThirdName End As LastName From SplitValues As FirstName Left Join SplitValues As SecondName On S2.Name = S1.Name And S2.Num = 2 Left Join SplitValues As ThirdName On S2.Name = S1.Name And S2.Num = 3 Where FirstName.Num = 1 </code></pre> <p>Here's a sample split function:</p> <pre><code>Create Function [dbo].[udf_Split] ( @DelimitedList nvarchar(max) , @Delimiter nvarchar(2) = ',' ) RETURNS TABLE AS RETURN ( With CorrectedList As ( Select Case When Left(@DelimitedList, Len(@Delimiter)) &lt;&gt; @Delimiter Then @Delimiter Else '' End + @DelimitedList + Case When Right(@DelimitedList, Len(@Delimiter)) &lt;&gt; @Delimiter Then @Delimiter Else '' End As List , Len(@Delimiter) As DelimiterLen ) , Numbers As ( Select TOP( Coalesce(DataLength(@DelimitedList)/2,0) ) Row_Number() Over ( Order By c1.object_id ) As Value From sys.columns As c1 Cross Join sys.columns As c2 ) Select CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen As Position , Substring ( CL.List , CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen , CharIndex(@Delimiter, CL.list, N.Value + 1) - ( CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen ) ) As Value From CorrectedList As CL Cross Join Numbers As N Where N.Value &lt;= DataLength(CL.List) / 2 And Substring(CL.List, N.Value, CL.DelimiterLen) = @Delimiter ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload