I already have code for pulling formatted dates from text fields such as the example below:
SUBSTRING(REPLACE(al.Comments + '.','.','{br}'),
PATINDEX('%Visit Date%',REPLACE(al.Comments+'.','.','{br}')) + 13,
PATINDEX('%{br}%',
SUBSTRING(REPLACE(al.Comments+'.','.','{br}'),
PATINDEX('%Visit Date%',
REPLACE(al.Comments+'.','.','{br}')) + 13, 14))-1))
The date is inserted programmatically into a Comments field of internal software. My question is how to pull a date from the same table and column (al.Comments) when the comment and the date are input manually by an end user.
Possible ways an end user might input a date would be:
- m/d/yy
- mm/dd/yy
- m/d/yyyy
- mm/dd/yyyy
- m/d (assume the same year as current year)
- mm/d (assume the same year as current year)
- m/dd (assume the same year as current year)
- mm/dd (assume the same year as current year)
Things to note
- This is SQL 2008
- SQL CLR and Regex are not possible
- I cannot request that the software be updated for this process
- I understand that this is complex and it may not be possible to account for all cases so anything that approximates as many of the possible end user inputs will be acceptable
In the case of this date (unlike the sample I posted above) I will be looking for a keyword of 'proposed' followed by a date and any other number of characters. Below is a sample of some of what I see in my results set for al.Comments:
I am creating a tracker in SSRS and among many other tables I am creating a result set that would allow me to track any proposed dates within a week or less of the date proposed. Most of my other result sets are either using datetime stamps or are pulling from programmatically added dates like the sample I posted. I can't figure out a way in SQL to pull this date so I can create a column for my results set/SSRS tracker
EDIT:
I have loosely called this a 'form' when in fact it is internal software that has a dev team that DOES have access to it, but this is part of a process and a database that is complex and has a lot of history. This department is one of many, all of our software outside of standard Google and Microsoft software is created internally. We have dozens of pieces of software that are all inter related and tied to each other in a very large, very complex database. I didn't write the software and the team that is over this software has bigger fish to fry that to add a field for my tracker to work.
This department is experiencing growing pains (a good thing) and just restructured all job functions last month. I work for a reports team and I was assigned a report to develop to track jobs assigned to technicians in the field.
If I have to write a complex, long SUBSTRING I will. Department standards do require that my queries are optimized and very fast. So I was looking for a way to query the database and parse text as simply as possible.
EDIT 2 (Testing Alan Burstein's answer below)
I have been testing Alan's answer and it is close. I could use a little help getting to the finish line. Below are some snippets of test code and the results I am getting.
SELECT
SUBSTRING(al.Comments,PATINDEX('%proposed%',al.Comments)+9,17) [col1]
INTO
#test1
Followed by this code:
SELECT
CASE
WHEN col1 LIKE '%/%-%/%' THEN SUBSTRING(#test1.col1,PATINDEX('%/%',#test1.col1)-2,5)
WHEN col1 LIKE '%/%,%/%' THEN SUBSTRING(#test1.col1,PATINDEX('%/%',#test1.col1)-2,5)
WHEN col1 LIKE '%/%/%' THEN SUBSTRING(#test1.col1,PATINDEX('%/%/%',#test1.col1)-2,12)
END
FROM
#test1
Sample of the results I am getting is below this - I haven't done the CAST yet because I have some leading spaces and trailing text I need to figure out how to exclude - thoughts?
This will be in a temp table and a LEFT JOIN so NULLs are fine.
EDIT 3 - How I got it to work w/o a function
I marked Alan's answer as correct because it worked when tested but I was told no functions by our Data Team Lead. I found another solution on StackExchange Read the second answer by G Mastros. I applied this next query to my previous one to strip out the offending characters:
SELECT
LEFT(SUBSTRING(#test2.col1, PATINDEX('%[0-9/]%', #test2.col1), 10),
PATINDEX('%[^0-9/]%', SUBSTRING(#test2.col1, PATINDEX('%[0-9/]%', #test2.col1), 10) + 'X')-1)
FROM #test2
The new results:


