使用 SQL 服务器从具有多个日期的字符串中提取最小日期
Extract Min Date from a string with several dates using SQL Server
我正在尝试从 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
在其他领域
如何将代码更正为仅 return 每个记录字段中的最小日期?
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
约翰的post超出了我目前的能力范围
我已经创建了函数,下面是提取数据的代码
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 table/field 放入 select 语句中。
超级快速草稿 - 使用 CHARINDEX 和 LEFT 检索第一个 space 之前的所有字符,然后将该文本转换为日期,然后使用 MIN 到 select 最早日期。
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
下面使用字符串解析器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
Returns
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
Returns
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
我不知道这将如何在大型数据集上执行
我正在尝试从 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
在其他领域
如何将代码更正为仅 return 每个记录字段中的最小日期?
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
约翰的post超出了我目前的能力范围
我已经创建了函数,下面是提取数据的代码
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 table/field 放入 select 语句中。
超级快速草稿 - 使用 CHARINDEX 和 LEFT 检索第一个 space 之前的所有字符,然后将该文本转换为日期,然后使用 MIN 到 select 最早日期。
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
下面使用字符串解析器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
Returns
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
Returns
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
我不知道这将如何在大型数据集上执行