title: Trying to extract text using CHARINDEX ()- 1 but getting an error
body: <p>I have a column with Names, and I am trying to split the column into First and Last Name using Text functions such as LEFT/SUBSTRING/CHARINDEX.</p>
  <p>Data in the column:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Name</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Yang, Jon</td>
  </tr>
  <tr>
  <td>Huang, Eugene</td>
  </tr>
  <tr>
  <td>Torres, Ruben</td>
  </tr>
  <tr>
  <td>Zhu, Christy</td>
  </tr>
  <tr>
  <td>Johnson, Elizabeth</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Everything works fine as long as I use this code:</p>
  <pre><code>SELECT 
      [Name]
      --,LEFT([Name], CHARINDEX(' ', [Name])) AS FirstName
      ,SUBSTRING([Name], 1, CHARINDEX(' ', [Name] )) AS FirstName
  FROM 
      DataModeling.Customer
  </code></pre>
  <p>But the problem arises when I try to subtract 1 from CHARINDEX to exclude the Comma from the result and it throws this error:
  <a href="https://i.stack.imgur.com/YwZIV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YwZIV.png" alt="enter image description here" /></a></p>
  <p>I have done this operation many times in Excel so trying to replicate it with TSQL. Any suggestion on what I am doing wrong is helpful.</p>