Solved

NEED SQL QUERY

Posted on 2012-04-12
3
345 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 60
query optimization 6 15
SQL Syntax Grouping Sum question 7 27
SQL - Simple Pivot query 8 15
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

830 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