title: SQL Server: retrieving the first word before the space or line break
body: <p>I have the following data set.</p>
  
  <pre><code>incidentid      calltype
  ----------      ---------
  _1700528604     101A1 (Investigation)
  _1700528605
  _1700528606     304B2
                  Problem
  </code></pre>
  
  <p>As it is shown, the calltype data for _1700528606 has Problem in the next line. I need to retrieve the first word from each callType, so that the end result will look like:</p>
  
  <pre><code> incidentid      calltype
  ----------      ---------
  _1700528604     101A1 
  _1700528605
  _1700528606     304B2
  </code></pre>
  
  <p>I used the partindex() as below.</p>
  
  <pre><code>select patindex( ('%[' + char(10) + ' ]%'), calltype)
  from #DispatchCallType 
  where IncidentId in (
  '_1700528604',
  '_1700528605',
  '_1700528606'
  )
  </code></pre>
  
  <p>The result was:</p>
  
  <pre><code>6
  0
  8
  </code></pre>
  
  <p>So I ran the following query.</p>
  
  <pre><code>SELECT incidentid,
   CASE 
    WHEN patindex( ('%[' + char(10) + ' ]%'), calltype) &gt; 0
    THEN LEFT(callType, charindex(' ', callType) - 1)
   ELSE callType 
   END AS callType
  from #DispatchCallType 
  where IncidentId in (
  '_1700528604',
  '_1700528605',
  '_1700528606'
  )
  </code></pre>
  
  <p>The above query returns data like this.</p>
  
  <pre><code>incidentid      calltype
  ----------      ---------
  _1700528604     101A1 (Investigation)
  _1700528605
  _1700528606     304B2
  </code></pre>