all groups > sql server programming > november 2005 >
You're in the

sql server programming

group:

Extract specific data from a ROW


Extract specific data from a ROW Rustom
11/15/2005 8:08:57 PM
sql server programming:
Hello,

I need some assistance with some T-SQL programming. I have a column called
"Message" and the following data is stored in this column. (See Below)

Logon Failure: Reason: Account locked out User Name: adubey
Domain: TSI Logon Type: 3 Logon Process: NtLmSsp Authentication
Package: NTLM Workstation Name: ITRJOHANNESON01 Caller User Name: -
Caller Domain: - Caller Logon ID

How would I manipulate this row to only extract the User Name and Domain and
insert the output into another column.

Thanks

Rustom

Re: Extract specific data from a ROW Ben Nevarez
11/15/2005 9:56:20 PM

If the data you are looking for is always on the same column just use the
substring function.
Otherwise, use also charindex and len functions (in addition to substring).

Ben Nevarez



[quoted text, click to view]

Re: Extract specific data from a ROW Adam Machanic
11/16/2005 12:42:28 AM
Something like:

SELECT
SUBSTRING(Message, UserIdx, CHARINDEX(CHAR(13), Message, UserIdx) -
UserIdx) AS UserName,
SUBSTRING(Message, DomainIdx, CHARINDEX('Caller Logon ID', Message,
DomainIdx) - DomainIdx) AS Domain
FROM
(
SELECT
CHARINDEX('Caller User Name: ', Message) + LEN('Caller User Name: ')
AS UserIdx,
CHARINDEX('Caller Domain: ', Message) + LEN('Caller Domain: ') AS
DomainIdx,
Message
FROM YourTable
) x

You should probably normalize this data. But that should be obvious at this
point :)


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--


[quoted text, click to view]

Re: Extract specific data from a ROW Rustom
11/16/2005 1:25:49 PM
Thanks for the feedback...I believe MS should normalize their data as this
is a MOM 2005 operational database.

lol

[quoted text, click to view]

Re: Extract specific data from a ROW Adam Machanic
11/16/2005 4:41:39 PM
[quoted text, click to view]

I agree! That's rough.


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--

Re: Extract specific data from a ROW Rustom
11/17/2005 1:35:05 PM
I am challenged here and require further assistance. So I have included two
rows of output of this one column called message. The data such as Logon
Failure, Reason and User Name never changes, however, the data after the :
does. So for this example, how would I write the transact SQL to extract
Account Locked out or Invalid Logon Attempt. Also this a MOM 2005 database
so I am not the crazy one here storing unnormalized data this way ;)

ROW 1 (Logon Failure: Reason: Account locked out User Name:
adubey)
ROW 2 (Logon Failure: Reason: Invalid Logon Attempt User Name: RKM)


Thanks Much!!!





[quoted text, click to view]

Re: Extract specific data from a ROW Stu
11/19/2005 4:34:09 PM
Hey Rustom,

One of our apps reads event log data as well, and I've written
1000-line views to do what you're trying to do. The basic formula
(using CHARINDEX and SUBSTRING) that others have given you will work,
but if your data increases, performance will begin to suffer.

You may want to consider using DTS to load your event files, and
transform them from the event log format to a denormalized, but
segregated tabular format. It'll take a little more time to load, but
will make your reporting job significantly easier.

HTH,
Stu
Re: Extract specific data from a ROW Hugo Kornelis
11/20/2005 12:47:13 AM
[quoted text, click to view]

Hi Rustom,

SELECT SUBSTRING(mess,
CHARINDEX('Reason: ', mess) + 9,
CHARINDEX('User Name: ', mess) - CHARINDEX('Reason: ',
mess) - 9)
FROM (SELECT 'Logon Failure: Reason: Account locked out User
Name: adubey' AS mess
UNION ALL
SELECT 'Logon Failure: Reason: Invalid Logon Attempt User
Name: RKM') AS testdata


Best, Hugo
--

AddThis Social Bookmark Button