title: Patindex Extract Date with either M/D/YY or MM/DD/YY
body: <p>I have the following code which extracts the date from a string and saves it to a temp table:</p>
  <p>I need the patindex to be flexible to extract 2 digit day and 2 digit month as well. The example below has just one digit month.</p>
  <pre><code>DECLARE @Stringval VARCHAR(500);
  DECLARE @Dateval DATETIME;
  
   DECLARE @DATETIME_TBL TABLE
   (
      datetime_val DATETIME 
   )
  
  
  SET @Stringval = 'Cairo Egypt - 1-15-10 System 1..' 
  
  IF ISDATE(SUBSTRING(@Stringval, patindex('%[0-9][0-9]-[0-9][0-9]-[0-9][0-9]%', @Stringval), 8)) = 1
  BEGIN
    INSERT INTO @DATETIME_TBL
    VALUES (CONVERT(DATETIME, SUBSTRING(@Stringval, patindex('%[0-9][0-9]-[0-9][0-9]-[0-9][0-9]%', @Stringval), 8)))
  END
  
  Select * from @DateTIME_TBL
  </code></pre>
  <p>I understand that for 1-15-10, it may need just one [0-9] but anyway we can have it flexible to include 2 digit month and 2 digit day all in one patindex</p>