How to get specific string/Date from a string in SQL Server using TSQL
Questions: How you will get all dates from below string, I have also include answer, it is done by recursive CTE

https://www.interviewquestionspdf.com/2017/09/how-to-get-specific-stringdate-from.html
DECLARE @S VARCHAR(MAX) =
ReplyDelete'09/14/2017:Your SR is ON HOLD - This is the final reminder to provid09/15/2017e 09/13/2017:Your
09/12/2017:Your SR is ON HOLD - We require the clarification from you on 254119.
A SR has been received by DMS and its execution09/18/2017 is in progress.
A SR has been received by SDS and09/25/2017 execution has not yet started on it.
A SR has been received by SDS and execution has not yet started on it.'
;WITH CTE AS
(
SELECT SUBSTRING(@S,CHARINDEX('/2017',@S)-5,10) AS [Date] ,SUBSTRING(@S,CHARINDEX('/2017',@S)+5,LEN(@S)) AS S
UNION ALL
SELECT SUBSTRING(S,CHARINDEX('/2017',S)-5,10) AS [Date],SUBSTRING(S,CHARINDEX('/2017',S)+5,LEN(S))
AS S FROM CTE WHERE CHARINDEX('/2017',S) > 0
)
SELECT * FROM CTE
very helpful.
ReplyDeleteThanks admin