I've got a column that stores text and I'm trying to find a way to pull the datetime from the field. I've tried to use a combination of SUBSTRING,CHARINDEX, and LEN to pull the datetime from the field - but not always.
And I tried to use the function I found in this SO thread - but it only seems to work to pull the date itself and not the time.
Below is some sample data and the code I'm using to pull the datetime from the field.
DROP TABLE IF EXISTS #temptable;
CREATE TABLE #temptable
(
ImportedBy VARCHAR(8000) NOT NULL
);
INSERT INTO #temptable
(
ImportedBy
)
VALUES
('Tttttt Tooooon 2/3/2022 8:54:16 AM'),
('Pyyyyyy Vyyyyyy RN 1/24/2022 2:42:30 PM'),
('Jul 18 2022 11:34AM'),
('Jul 13 2022 10:32AM'),
('Meeeen Tooooo LPN 6/20/2022 3:57:15 PM'),
('Aaaaa Leeers RN 1/23/2023 9:48:21 AM');
SELECT t.ImportedBy,
CASE
WHEN TRY_CAST(t.ImportedBy AS DATETIME) IS NOT NULL THEN
t.ImportedBy
ELSE
SUBSTRING(
TRIM(RIGHT(t.ImportedBy, 25)),
CHARINDEX(' ', TRIM(RIGHT(t.ImportedBy, 25))),
(LEN(TRIM(RIGHT(t.ImportedBy, 25))) - (CHARINDEX(' ', TRIM(RIGHT(t.ImportedBy, 25))))) + 1
)
END AS [Scanned Date And Time],
TRIM(RIGHT(t.ImportedBy, 25)) AS Step1,
CHARINDEX(' ', TRIM(RIGHT(t.ImportedBy, 25))) AS Step2,
(LEN(TRIM(RIGHT(t.ImportedBy, 25))) - (CHARINDEX(' ', TRIM(RIGHT(t.ImportedBy, 25))))) + 1 AS Step3,
TRY_CAST(TRIM(ps.Item) AS DATE) AS ScannedDate
FROM #temptable AS t
CROSS APPLY dbo.PatternSplitCM(t.ImportedBy, '[0-9/]') AS ps
WHERE ps.Matched = 1
AND ps.Item LIKE '%[0-9]/[0-9]%';
I'm not sure what the solution might be - if the user doesn't enter a title behind their name - like in row 1 - it appears to work. But if the user has "RN" after this name, it isn't working correctly but "LPN" doesn't seem to cause an issue. And I don't understand why that would be. And the PatternSplitCM function does work - but it is required to also include the time stamp so that doesn't appear to be a resolution.
Looking at the results when I added in each part of the SUBSTRING -
- The RIGHT, 25 takes the rightmost 25 characters of the string
- I'm not not sure what this CHARINDEX is doing
- I'm not sure what this is doing either - LEN - CHARINDEX(RIGHT)
The function is in the linked thread but I'll post it here as well:
-- Function by Chris Morris, read more here: http://www.sqlservercentral.com/articles/String+Manipulation/94365/
CREATE FUNCTION dbo.PatternSplitCM
(
@List VARCHAR(8000) = NULL
,@Pattern VARCHAR(50)
) RETURNS TABLE WITH SCHEMABINDING
AS RETURN
WITH numbers AS (
SELECT TOP(ISNULL(DATALENGTH(@List), 0))
n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n))
SELECT
ItemNumber = ROW_NUMBER() OVER(ORDER BY MIN(n)),
Item = SUBSTRING(@List,MIN(n),1+MAX(n)-MIN(n)),
[Matched]
FROM (
SELECT n, y.[Matched], Grouper = n - ROW_NUMBER() OVER(ORDER BY y.[Matched],n)
FROM numbers
CROSS APPLY (
SELECT [Matched] = CASE WHEN SUBSTRING(@List,n,1) LIKE @Pattern THEN 1 ELSE 0 END
) y
) d
GROUP BY [Matched], Grouper;

selectto returnps.*and each of the expressions in thecaseexpression. It may help to add characters to bracket the string values so that you can see any extra whitespace. If you still can't debug the code then please edit your post to include thePatternSplitCMfunction definition.SUBSTRING()code is returning everything after the first space in the last 25 characters of the column.PATINDEX(). If you were using a newer version of SQL-Server you could useREGEXP_SUBSTR().