T-SQL - 删除字符串

时间:2018-03-01 15:03:01

标签: sql sql-server tsql

请帮助我查看下面显示的SQL查询。

这是我得到的错误:

  

System.Exception:TransformerException ---> Library.Utilities.TransformerException:变换器使用[4:transformId]转换[00000000000 85153227 :documentId],剩余时间[00:00:54.4517645:TimeSpan]输出文档为[0000000000085153231:documentId] --- >等等...

我正在训练只检索此值85153227。

请协助

SELECT  
    SUBSTRING(message, 0, CHARINDEX('System.Exception', message)) + ' ' +  
    CASE          
       WHEN CHARINDEX('Transformer transforming [', message) > 0
          THEN SUBSTRING(message, CHARINDEX('[',message), LEN(message)-CHARINDEX('[', message)) 
          ELSE '' 
    END
FROM
    dbo.Documents 

谢谢

5 个答案:

答案 0 :(得分:1)

DECLARE @ AS VARCHAR(MAX)= 
'System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId]'

SELECT 
CAST(
SUBSTRING(@,
    CHARINDEX('Transformer transforming [',@,0) + LEN('Transformer transforming [')
,   CHARINDEX(':documentId]',@,0) - ( CHARINDEX('Transformer transforming [',@,0) + LEN('Transformer transforming [') ))
AS BIGINT)

- 输出

--------------------
85153227

(1 row affected)

答案 1 :(得分:0)

很难确定对此的确切响应,因为您没有确定异常是否始终结构相同。有了所提供的,这就是我要做的。

  1. 删除STUFF的前导字符串; STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N'')
  2. 然后假设带有数字的字符串长度为19个字符,然后我会SUBSTRING找到第一个非零字符。 SUBSTRING({field}, PATINDEX('%[^0]%', {field} + '.'), LEN({field}))将第1步替换为{field}的字符串(我试图在此处简化代码)。
  3. 这是显示此内容的SQL语句。

    IF OBJECT_ID(N'tempdb..#t') IS NOT NULL DROP TABLE #t
    SELECT CAST('System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId] ' AS NVARCHAR(MAX))
        AS [Message]
        INTO #t
    
    SELECT *,
        STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N'') AS BeginningCharactersStripped,
        SUBSTRING(LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19), PATINDEX('%[^0]%', LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19) + '.'), LEN(LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19)))
            AS Number
    FROM #t
    WHERE [Message] like N'%Transformer transforming [[]%'
    

答案 2 :(得分:0)

根据您提供的内容,我稍微修改了您的查询以获得您想要的输出。您可能需要根据数据进行调整。

SELECT  substring(message,0,charindex('System.Exception',message)) + ' ' + 
CASE          WHEN charindex('Transformer transforming [',message) > 0
          THEN Convert(Varchar(8000), Convert(BigInt, Substring(message,charindex('[',message) + 1, charindex(':documentid]',message)-(CharIndex('[',message)+1)))) ELSE '' END
FROM dbo.Documents 

答案 3 :(得分:0)

你可以试试这个:

with MsgWithZeroesTbl as 
(SELECT  'System.Exception ' + 
        CASE      
            WHEN charindex('Transformer transforming [',message) >= 0
            THEN substring(message,charindex('[',message) + 1, charindex(':',message, charindex('[',message))- (charindex('[',message) + 1)) ELSE '' 
        END as MsgWithZeros
        FROM dbo.Documents )
Select substring(MsgWithZeros, patindex('%[^0]%',MsgWithZeros), 10) from MsgWithZeroesTbl

答案 4 :(得分:0)

我想出了:

DECLARE @string varchar(8000) = 
'System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId] --->'

SELECT cast(substring(@string, p1.p, p2.p-p1.p) as bigint)
FROM (VALUES (patindex('%[0-9]%', @string))) p1(p)
CROSS APPLY (VALUES (charindex(':', @string, p1.p+1))) p2(p);

输出:

85153227