title: SQL Server: how can I combine the CHARINDEX, LEFT, and RIGHT functions into one expression?
body: <p>I have the following SQL expressions, which do exactly what I need them to:</p>
  
  <pre><code>DECLARE @ServerName NVARCHAR(20)
  
  SET @ServerName = RIGHT(@@SERVERNAME, LEN( @@SERVERNAME) - CHARINDEX('-', @@SERVERNAME))
  
  SET @ServerName = LEFT(@ServerName, LEN(@ServerName) - 2)
  
  SELECT @ServerName;
  </code></pre>
  
  <p>However, I'd like to combine the two SET expressions into one if possible. I've tried doing it on my own, but the <code>CHARINDEX</code> section throws me off. How can I combine those two so that I have one <code>SET</code> expression that does all of the work on the string at once?</p>