I have a very specific requirement in SQL Server 2012 where I need to match a given string to the specific date format of 'MM/dd/yyyy'. I do not need to CONVERT or CAST the string to a date, just a simple 1 or 0. Is the string in that format or not.
Example:
| ID | Date | Result |
|----|------------|--------|
| 1 | 05/31/2022 | 1 |
| 2 | 31/05/2022 | 1 |
| 3 | 2022/05/31 | 0 |
| 4 | 5/31/2022 | 0 |
| 5 | 5/31/22 | 0 |
| 6 | qwerty | 0 |
| 7 | 5.31.2022 | 0 |
| 8 | 5-31-2022 | 0 |
| 9 | May/31/2022| 0 |
I had assumed a simple regex would work but I have not been able to find one that works in SQL. Currently I attempting to use '^[0-9]{2}/[0-9]{2}/[0-9]{4}' but this is failing for all values. I feel like I am missing something very simple but I cannot figure out what it is, and regex is not my strong suit as well.
Based on the comments i need to clarify something. The validation that is being done is 2 fold.
First, is the string a valid date, I have that covered no problem so I did not think to add that to this question originally.
Second, is the string in the appropriate format "2 digits + '/' + 2 digits + '/' + 4 digits". This second step is where I am requiring assistance.
Any guidance would be appreciated.