title: Return null from SUBSTRING if substring not found
body: <pre><code>SELECT
  SUBSTRING(campaign_name,CHARINDEX(‘[’,campaign_name)+1,
  (((LEN(campaign_name))-CHARINDEX(‘]’,REVERSE(campaign_name)))-
  CHARINDEX(‘[’,campaign_name))) as campaign_code
  FROM table
  </code></pre>
  
  <p>This code currently gives me the substring between '[' and ']' from the field campaign_name. However, for a couple of entries in the campaign_name field there is no '[' or ']'. In these cases, the above code returns the whole string in the field. Instead I want to return NULL but at the moment I have been unable to execute this. I would love some thoughts.</p>