I have a text field that contains multiple datetime stamps and I need to remove just the time stamp. There is also text with carriage returns in this field. I want to maintain text, date, and carriage returns.
Anyone have experience with this and can share the select to accomplish this?
Here is an example of the textfield :
[10/19/2015 5:24:02 PM Tech1] Repaired heaters [10/21/2015 8:36:28 AM Tech1] CHECKED ALL HEATER OPERATION
Any insight would be greatly appreciated. Thanks in advance for your help!
Here's a way with STUFF
and a splitter function, taking into account single and double digit hours.
declare @table table (vars varchar(4000))
insert into @table
values
('[10/19/2015 5:24:02 PM Tech1] Repaired heaters' + char(10) + '[10/21/2015 12:36:28 AM Tech1] CHECKED ALL HEATER OPERATION' + char(13) + '[10/22/2015 3:36:28 PM Tech1] somethingelse')
,('[11/19/2015 5:24:02 PM Tech1] asdfsdaf [11/21/2015 8:36:28 AM Tech1] dddddddddddd [11/22/2015 3:36:28 PM Tech1] yyyyyyyyy')
,('[1/19/2015 11:24:02 PM Tech1] here went something [09/21/2015 08:36:28 AM Tech1] Tech 1 did something else[11/22/2015 10:36:28 PM Tech1] last')
;with cte as(
select
vars
,RN = dense_rank() over (order by vars)
--,Item
,Item = '[' + stuff(Item,charindex(' ',Item), 3 + case when charindex(' AM ',Item) = 0 then charindex(' PM ',Item) else charindex(' AM ',Item) end - charindex(' ',Item),'')
from
@table
cross apply
dbo.DelimitedSplit8K(vars,'[')
where
Item <>'')
select distinct
vars
,RemovedTime =
STUFF((
SELECT ' ' + c2.Item
FROM cte c2
WHERE c.RN = c2.RN
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from
cte c
The splitter can be found in Jeff Moden's Article
CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l
;
GO
UPDATE: Misunderstood the requirement, updated my solution based on comments below.
For this you will need PatternSplitCM.
declare @table table (someid int identity, somestring varchar(1000));
insert @table(somestring) values
('[10/19/2015 5:24:02 PM Tech1] Repaired heaters [10/21/2015 8:36:28 AM Tech1] CHECKED ALL HEATER OPERATION'),
('[02/28/2015 5:24:02 PM Tech1] Repaired more stuff [12/01/2015 2:36:28 AM Tech1] CHECKED ALL HEATER OPERATION');
with parseMe as
(
select
someid,
ItemNumber,
Item = case
when Item like '[0-9]:[0-9][0-9]:[0-9][0-9]' then '<exclude>'
else replace(replace(item, ' AM ',''), ' PM ', '') end
from @table t
cross apply dbo.PatternSplitCM(t.somestring, '[0-9:/]')
)
select
someid,
newString =
replace((
select item+''
from parseMe p2
where p1.someid = p2.someid
order by ItemNumber
for xml path(''), type
).value('.', 'varchar(1000)'), '<exclude>','')
from parseMe p1
group by someid;