I am trying to extract the date from a string. I am using the below mentioned query using both SUBSTRING and PATINDEX:
SELECT Message, SUBSTRING(Message, PATINDEX('%[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]%', Message), 10) as Month_Captured
I am confused by the results "CONTROL V", please see the attached image. Can you please explain why this is happening and what is the best way to extract the date from a string.
The easiest way to handle a no-match case from CHARINDEX is to coerce the 0 to a null with the
NULLIF(value, 0)
function. This will in turn cause the SUBSTRING to also yield null.