SQL从字符串的任何部分替换/删除多个日期和时间戳

时间:2017-05-09 15:21:42

标签: sql sql-server tsql datetime replace

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

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

请允许任何人分享我如何在包含此字符串的示例备注字段中编写我的选择:

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

enter image description here

1 个答案:

答案 0 :(得分:2)

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

示例

function mainLoop(time){
   if(mouse.b1){  // is button 1 down
       ctx.beginPath();
       ctx.moveTo(mouse.lastX,mouse.lastY);
       ctx.lineTo(mouse.x,mouse.y);
       ctx.stroke();
   }


   // save the last known mouse coordinate here not in the mouse event
   mouse.lastX = mouse.x;
   mouse.lastY = mouse.y;
   requestAnimationFrame(mainLoop); // get next frame
}
// start the app
requestAnimationFrame(mainLoop);

<强>返回

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
  

编辑 - 具有解析功能的选项2

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

感兴趣的UDF

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