【问题标题】:SQL replace/ remove multiple date and time stamp from any part of a stringSQL 从字符串的任何部分替换/删除多个日期和时间戳
【发布时间】:2017-10-07 23:59:12
【问题描述】:

我有一个 nvarchar 字段,其中包含多个日期和时间戳以及各种文本。日期和时间可以在字段中的任何位置。

我只想从字段中选择文本。我尝试使用 REPLACE 和 PATINDEX 无济于事。

请任何人分享我将如何在包含此字符串的示例注释字段上编写我的选择:

ADMIN1 21/04/2017 02:01:01 这位学生在这里并试图获得硕士学位。 ITSYS2 09/05/2017 03:51:04 xout 上的 60 个 APL 积分

【问题讨论】:

  • 没有其他可以解析的分隔符吗?制表符还是换行符?
  • 我曾想过在每个空格处拆分字符串,但用户输入数据的方式并没有真正的模式。

标签: sql sql-server tsql datetime replace


【解决方案1】:

以下内容将从 note_detail 中排除日期和时间。这是一种内联方法,但几乎任何拆分/解析函数都可以解决问题。

示例

Declare @YourTable table(studend_id int,note_detail varchar(max))
Insert Into @YourTable values
(1,'CHIDLOL 21/04/2017  02:01:01 '+CHAR(13)+CHAR(10)+'This studend is here and trying to gain a masters.  THOMASXC 09/05/2014 03:54:04 60 APL Credon on xout')

Select A.studend_id
      ,new_note_detail = B.S
 From  @YourTable A
 Cross Apply (
                Select S = Stuff((Select ' ' +RetVal 
                  From (
                        Select RetSeq = Row_Number() over (Order By (Select null))
                              ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace((Select replace(replace(replace(A.note_detail,char(13),' '),char(10),' '),' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                        Cross Apply x.nodes('x') AS B(i)
                       ) B1
                  Where RetVal not like '%[0-9]/[0-9][0-9]/[0-9]%'
                    and RetVal not like '%[0-9]:[0-9][0-9]:[0-9]%'
                  Order by RetSeq
                  For XML Path ('')),1,1,'') 
             ) B

退货

studend_id  new_note_detail
1           CHIDLOL This studend is here and trying to gain a masters. THOMASXC 60 APL Credon on xout

编辑 - 带有解析函数的选项 2

Select A.studend_id
      ,new_note_detail = B.S
 From  @YourTable A
 Cross Apply (
                Select S = Stuff((Select ' ' +RetVal 
                  From [dbo].[udf-Str-Parse](replace(replace(A.note_detail,char(13),' '),char(10),' '),' ') B1
                  Where RetVal not like '%[0-9]/[0-9][0-9]/[0-9]%'
                    and RetVal not like '%[0-9]:[0-9][0-9]:[0-9]%'
                  Order by RetSeq
                  For XML Path ('')),1,1,'') 
             ) B

有兴趣的 UDF

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table 
As
Return (  
    Select RetSeq = Row_Number() over (Order By (Select null))
          ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
    From  (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
    Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')

【讨论】:

  • 嗨,约翰,谢谢您的回复,当我应用此代码时,日期和时间戳将替换为这些字符
  • @clare 这些是 CRLF。你想换行还是去掉它们?
  • 嗨,约翰,是的,我也希望删除 CRLF
  • @clare 更新了这两个选项。
  • 效果很好。我使用了内联解决方案谢谢。
猜你喜欢
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 2017-06-21
相关资源
最近更新 更多