Solved

NEED SQL QUERY

Posted on 2012-04-12
3
344 Views
Last Modified: 2012-04-12
I have a column like below, i need query which will pick up Char after event number dot(.) still before ;
i.e after Event17063. so first char will be "M" and take still "t".
result will be MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert and should grouped by this title.

Server Name : xxxxx; APP: Connectr; ShortDesc: Event17063.MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert;  Desc:...
Server: qeettdd; SourceApp:Connectr; ShortDesc: Event208.SQLSERVERAGENT.ppgTIPSR.SQLJobFailure.Alert;  Desc: ...
Server Name : abc; APP: Connectr; ShortDesc:..........
Event17063.MSSQLSERVER.Application.ppgTIPSR.Error50000Severity16State1.Alert;  Desc: .....................
Server Name : xxxxx; APP: Connectr; ShortDesc: Event17063.MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert;  Desc:...

Expected output:
Title                                                count
MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert                         2
SQLSERVERAGENT.ppgTIPSR.SQLJobFailure.Alert                   1
MSSQLSERVER.Application.ppgTIPSR.Error50000Severity16State1.Alert       1
0
Comment
3 Comments
 
LVL 12

Expert Comment

by:Anuradha Goli
ID: 37836536
The logic behind to retrieve data from such text is given below:
Order is my sample table with fulldata as column with long text .
DECLARE @INDEX1 INT 
DECLARE @INDEX2 INT 
DECLARE @INDEX3 INT 
DECLARE @STRING1 NVARCHAR(200)
DECLARE @STRING2 NVARCHAR(200) 
DECLARE @STRING3 NVARCHAR(200) 

SELECT @INDEX1 = Charindex('EVENT', fulldata) 
FROM   dbo.[ORDER] 
SELECT @STRING1 = Substring(fulldata, @INDEX1, ( Len(fulldata) - @INDEX1 + 1 )) 
FROM   dbo.[ORDER] 

SELECT @INDEX2 = Charindex('.', @STRING1) 
FROM   dbo.[ORDER] 
SELECT @STRING2 = Substring(@STRING1, @INDEX2 + 1, 
                  ( Len(@STRING1) - @INDEX2 + 1 )) 
FROM   dbo.[ORDER] 

SELECT @INDEX3 = Charindex(';', @STRING2) 
FROM   dbo.[ORDER] 
SELECT @STRING3 = Substring(@STRING2, 0, @INDEX3) 
FROM   dbo.[ORDER] 

SELECT @STRING3 

Open in new window


Hope this helps you retrieve data from your fields.
Just logic is to get data between first "." after EVENTxxxxx and before ";"
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
ID: 37837655
Replace #Events with your table name and "EventDescription" with your column name in below query:

SELECT SUBSTRING (SUBSTRING (EventDescription, PATINDEX ('%Event[0-9]%', EventDescription), CHARINDEX (';  Desc:', EventDescription) - PATINDEX ('%Event[0-9]%', EventDescription)),
      CHARINDEX('.', SUBSTRING (EventDescription, PATINDEX ('%Event[0-9]%', EventDescription), CHARINDEX (';  Desc:', EventDescription) - PATINDEX ('%Event[0-9]%', EventDescription))) + 1, 1000),
      COUNT (*)
FROM #Events
WHERE PATINDEX ('%Event[0-9]%', EventDescription) > 0
And CHARINDEX (';  Desc:', EventDescription) > 0
Group by SUBSTRING (SUBSTRING (EventDescription, PATINDEX ('%Event[0-9]%', EventDescription), CHARINDEX (';  Desc:', EventDescription) - PATINDEX ('%Event[0-9]%', EventDescription)),
      CHARINDEX('.', SUBSTRING (EventDescription, PATINDEX ('%Event[0-9]%', EventDescription), CHARINDEX (';  Desc:', EventDescription) - PATINDEX ('%Event[0-9]%', EventDescription))) + 1, 1000)

Thanks,
Harish
0
 
LVL 5

Author Closing Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 37840504
Excellent job!!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question