title: SQL if-else statement to check for a specific character
body: <p>I am a complete novice when it comes to SQL so I'm hoping someone can help me out, what I need to do is create an if-else statement that checks to see if a specific character exists ('/') if it does then ignore the text after this until a white space is found.</p>
  
  <p>When the white space is found only return the 5th and 6th characters. If no ('/) is found then simply return the 5th and 6th characters.</p>
  
  <p>My code what I have so far is below and I know this could be a lot cleaner but I am just now learning SQL so my apologies.</p>
  
  <pre><code>select top 10000
  full_message,
  
  SUBSTRING (full_message,CHARINDEX(':52A:', full_message) + 5,  CHARINDEX(':5', full_message, (CHARINDEX(':52A:', full_message)+1)) -  CHARINDEX(':52A:', full_message) - 5) as [CountryCode]
  
  -- create if-else statement to see if the first character is a '/' if it is from the white space take the 5th &amp; 6th characters.
  
  -- else take the 5th &amp; 6th characters  
  
  from [dbo].[tbl_Test] where CHARINDEX(':52A:', full_message) &gt; 0
  </code></pre>
  
  <p>My CountryCode column returns values like:-</p>
  
  <ol>
  <li><p>Value returned with a ('/RandomString')</p>
  
  <p>/04457120 GZQRUSAXXX</p></li>
  <li><p>Value returned without a ('/RandomString')</p>
  
  <p>LTDXGB7PXXX</p></li>
  </ol>
  
  <p>So ideally the CountryCode column will return</p>
  
  <ol>
  <li><p>Value returned with a ('/RandomString') then removed</p>
  
  <p>US</p></li>
  <li><p>Value returned without ('/RandomString')</p>
  
  <p>GB</p></li>
  </ol>
  
  <p>I hope this question makes sense and if you need me to better clarify something then please do I am using Microsoft SQL.</p>
  
  <p>Kind regards,</p>
  
  <p>Ryan</p>