简体   繁体   English

T-SQL从字符串中找到一个八个字符串,该字符串不包含空格并且至少包含一个数字,并将其排除在外

[英]T-SQL find an eight character string which contains no spaces and at least 1 number from within a string and exclude it

The problem is we have many different error messages being produced and stored in a sql table within the error message there could be an occurance of a project number 8 characters long which would contain at least 1 number is alphanumeric and no spaces. 问题是我们有许多不同的错误消息正在生成,并存储在错误消息内的sql表中,可能会出现一个项目编号为8个字符的项目,该项目将包含至少1个字母数字且没有空格。 per error message the project number may not be the same. 每条错误消息的项目编号可能不同。

eg 例如

'error found processing project: abcd12sf no funding is set'
'error found processing project: qd451srf no funding is set'
'error project 2344ddrf has no approver'

We want to be able to count the occurances of particular errors and so we need to strip the project number from the output 我们希望能够计算出特定错误的发生,因此我们需要从输出中剥离项目编号

thus 从而

'error found processing project: no funding is set' , 2 occurances
'error project has no approver' , 1 occurance

Any help is greatly appreciated 任何帮助是极大的赞赏

Start by creating a Function that returns the project number. 首先创建一个返回项目编号的函数。 I did this by finding the first integer and then position of the space after it, then counting back 8 characters you have the start and end position. 为此,我先找到第一个整数,然后找到其后的空格位置,然后算回8个字符,分别确定起始位置和结束位置。

CREATE FUNCTION GetProjectNumber(@FullErrorMessage varchar(100))

RETURNS CHAR(8)

AS 

BEGIN

    DECLARE @ProjectNumber CHAR(8)

    SET @ProjectNumber = (
                                    SELECT  
                                    SUBSTRING(@FullErrorMessage, CHARINDEX(' ',@FullErrorMessage,PATINDEX('%[0-9]%',@FullErrorMessage))-8,8)
                                )

    RETURN @ProjectNumber
END

Alter your table to have a computed column called ProjectNumber here is the create I did for testing but you should get the idea. 更改您的表以具有一个称为ProjectNumber的计算列,这是我为测试所做的创建,但您应该明白这一点。

CREATE TABLE T_Errors(
ID int IDENTITY(1,1),
ErrorMessage varchar(1024),
ProjectNumber AS (dbo.GetProjectNumber(ErrorMessage))
CONSTRAINT PK_T_Errors PRIMARY KEY CLUSTERED(ID))

Now you have the project number in a separate column it is easy to play with. 现在,您可以在一个单独的栏中轻松找到项目编号。

SELECT 
CleanErrorMessage = REPLACE(ErrorMessage,ProjectNumber,''),
COUNT(*) As ErrorCount
FROM T_Errors
GROUP BY REPLACE(ErrorMessage,ProjectNumber,'')

If you have a list of valid project numbers you can do something like this. 如果您有有效项目编号的列表,则可以执行以下操作。

Sample tables and data: 样本表和数据:

declare @Project table
(
  ProjectNumber char(8)
)

insert into @Project values
('abcd12sf'),
('qd451srf'),
('2344ddrf')

declare @Error table
(
  Error varchar(100)
)

insert into @Error values
('error found processing project: abcd12sf no funding is set'),
('error found processing project: qd451srf no funding is set'),
('error project 2344ddrf has no approver')

The query: 查询:

select P.Error,
       count(*) as ErrorCount
from @Error as E
  cross apply
    (
      select replace(E.Error, P.ProjectNumber, '') as Error
      from @Project as P
      where E.Error like '%'+P.ProjectNumber+'%'
    ) as P
group by P.Error

Result: 结果:

Error                                               ErrorCount
--------------------------------------------------  ----------
error found processing project:  no funding is set  2
error project  has no approver                      1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM