title: Fastest way to check if a character is a digit?
body: <p>I am having issues with sqlserver's ISNUMERIC function where it is returning true for ','  </p>
  
  <p>I am parsing a postal code and trying to see if the second char (supposed to be a digit) is a 0 or not and do something different in each case. The issue is that I can't just cast the char by checking isNumeric first. Here is the code for my scalar-valued function to return the digit in the second char location, and -1 if it is not a digit.   </p>
  
  <pre><code>@declare firstDigit int
  
  IF ISNUMERIC(SUBSTRING(@postal,2,1) AS int) = 1
     set @firstDigit = CAST(SUBSTRING(@postal,2,1) AS int)
  ELSE
     set @firstDigit = -1       
  
  RETURN @firstdigit
  </code></pre>
  
  <p>Since this fails when the postal code is not quite valid. I am just trying to find out how to check if the nvarchar @postal 's second character is a digit from 0-9. I have seen different types of solutions such as using <code>LIKE [0-9]</code> or using <code>PATINDEX</code> etc. </p>
  
  <p><b>Is there a better/easier way to do this, and if not which method will be the fastest?</b></p>
  
  <p>EDIT: Code added as per Aaron Bertrand's suggestion  </p>
  
  <pre><code>ON z.postal = 
     CASE
        WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
            THEN SUBSTRING(v.patientPostal, 2,1) END) = 0 then v.patientPostal
        WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
            THEN SUBSTRING(v.patientPostal, 2,1) END) &gt; 0 then LEFT(v.patientPostal,3)
  </code></pre>