title: SQL Server 2008 r2 - T-SQL LIKE or PATINDEX to match characters OTHER than A-Z, 0-9, hyphen, period, underscore and tilde
body: <p>Please help me write a PATINDEX or LIKE statement to match characters <strong>other</strong> than:</p>
  
  <ul>
  <li>A-Z, 0-9, hyphen (-), period (.), underscore (_), and tilde (~)</li>
  </ul>
  
  <p>I plan to use this in a scalar UDF with an nvarchar(200) input, which processes the input by:</p>
  
  <ol>
  <li>Replace the non-matching characters with hyphen (-)</li>
  <li>Replace two occurences of hyphen (--) with single (-)</li>
  <li>Removes leading and trailing hypens (-)</li>
  <li>Returns the processed input</li>
  </ol>
  
  <p>This will be used to create part of an SEO-friendly URL e.g. <code>/my-seo-friendly-url-1</code>. I am very confident in doing this UDF, apart from the pattern-matching part. Regex-like stuff confuses me! Please help.</p>
  
  <p>Thanks for your help in advance.</p>