1

我正在尝试从varchar字符串中提取最小日期。

字段中的数据如下所示

QTY DIFFERENCE - PO LINE 6.  147 ON PO / 192 ON INVOICE

5/18/2016 4:18:52 PM by ROOFING\ebuchanan
ANDREW SANTORI ISSUED THIS PO, PLEASE SEND TO HIS QUE

5/21/2016 9:48:42 AM by ROOFING\knaylor
RE-ROUTED TO ATS

使用此代码

SELECT 
    UISeq,
    LEFT(SUBSTRING(Notes, PATINDEX('%[0-9/]%', Notes), 8000),
       PATINDEX('%[^0-9/]%', SUBSTRING(Notes, PATINDEX('%[0-9/]%', Notes), 8000) + 'X') -1) as 'MaxDate'
FROM 
    bAPUI
WHERE
    Notes IS NOT NULL
ORDER BY
    UISeq

我从上面的记录中得到这个结果

6

我也得到

01/01/2000

在其他领域

如何更正代码以仅返回每个记录字段中的最小日期?

 UISeq  MinDate
  2      3
  3      5
 13   4/1/2016
 15      1
 17 
 18  4/15/2016
 19     3
 20  4/15/2016
 40  05/22/16
 43  05/22/16
 54  5/18/16

约翰的帖子超出了我目前的能力

我已经创建了函数,这是提取数据的代码

Declare @Str varchar(max);

Select @Str as Notes, Min(Key_Value)

from bAPUI, [dbo].[SA-udf-Str-Parse](replace(@Str,char(13),' '),' ')

Where Key_Value like '%/%'
  and len(Key_Value)>=10

我不明白的是如何将 bAPUI.Notes 表/字段放入 select 语句中。

4

2 回答 2

1

下面使用字符串解析器 udf。也许在您的数据中,甚至只是在示例中,有 chr(13),所以我必须执行 replace(),可能还有其他可能需要捕获的扩展字符。

Declare @Str varchar(max)
Set @Str='QTY DIFFERENCE - PO LINE 6.  147 ON PO / 192 ON INVOICE

5/18/2016 4:18:52 PM by ROOFING\ebuchanan
ANDREW SANTORI ISSUED THIS PO, PLEASE SEND TO HIS QUE

5/21/2016 9:48:42 AM by ROOFING\knaylor
RE-ROUTED TO ATS'

Select * from [dbo].[udf-Str-Parse](replace(@Str,char(13),' '),' ')
Where Key_Value like '%/%'
  and len(Key_Value)>=10

退货

Key_PS  Key_Value
13      5/18/2016
28      5/21/2016

快速更换

Select Min(Key_Value) from [dbo].[udf-Str-Parse](replace(@Str,char(13),' '),' ')
Where Key_Value like '%/%'
  and len(Key_Value)>=10

退货

5/18/2016

有数百万种变化,但这是我的。

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@delimeter varchar(10))
--Usage: 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]('id26,id46|id658,id967','|')

Returns @ReturnTable Table (Key_PS int IDENTITY(1,1) NOT NULL , Key_Value varchar(500))

As

Begin
   Declare @intPos int,@SubStr varchar(500)
   Set @IntPos = CharIndex(@delimeter, @String)
   Set @String = Replace(@String,@delimeter+@delimeter,@delimeter)
   While @IntPos > 0
      Begin
         Set @SubStr = Substring(@String, 0, @IntPos)
         Insert into @ReturnTable (Key_Value) values (@SubStr)
         Set @String = Replace(@String, @SubStr + @delimeter, '')
         Set @IntPos = CharIndex(@delimeter, @String)
      End
   Insert into @ReturnTable (Key_Value) values (@String)
   Return 
End

所以应用到你的数据

Select UISeq,
      ,MinDate=(Select Min(Key_Value) from [dbo].[udf-Str-Parse](replace(Notes,char(13),' '),' ') Where Key_Value like '%/%' and len(Key_Value)>=10) 
FROM  bAPUI
WHERE Notes IS NOT NULL
ORDER BYUISeq

我不知道这将如何在大型数据集上执行

于 2016-05-26T18:37:53.770 回答
0

超级快速草稿 - 使用 CHARINDEX 和 LEFT 检索直到第一个空格的所有字符,然后将该文本转换为 DATE,然后使用 MIN 选择最早的日期。

在此处输入图像描述

select @str as string ,left(@str,CHARINDEX(' ',@str)) -- Get the position of the first space, then select all characters up to the space ,MIN(convert(date,left(@str,CHARINDEX(' ',@str)))) -- Convert the selected characters to a date and then use MIN to select earliest date

于 2016-05-26T18:16:44.997 回答