Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:27:15+00:00 2026-06-19T03:27:15+00:00

I am trying to track SQL logins successes and failures and store them in

  • 0

I am trying to track SQL logins successes and failures and store them in a SQL table for querying purposes. I have written a query that works, but my problem is, when the login name contains the domain, it works fine, but if it does not contain a domain, it addess extra text. I was hoping someone could help me review my code and see how to remove the extra after the login name.

Example of what it returns:
Domain\BBruning

Domain\SQLServ

Domain\BSmith

Domain\JHarris’.

scgWeb’. Reason: F

cpadmin’. Reason:

CREATE TABLE [dbo].[#TmpErrorLog]
([LogDate] DATETIME NULL,
[ProcessInfo] VARCHAR(20) NULL,
[Text] VARCHAR(MAX) NULL);

CREATE TABLE [dbo].[#TmpErrorLog2]
([LogDate] DATETIME NULL,
[ProcessInfo] VARCHAR(20) NULL,
[Text] VARCHAR(MAX) NULL,
[LoginAttempt] VARCHAR(20) NULL,
[ServerName] VARCHAR(20) NULL);


INSERT INTO #TmpErrorLog ([LogDate], [ProcessInfo], [Text])
EXEC [master].[dbo].[xp_readerrorlog] 0 ;

INSERT INTO #TmpErrorLog2 ([LogDate], [ProcessInfo], [Text], [LoginAttempt])
Select LogDate, ProcessInfo, Text, SUBSTRING(Text,0,16) as LoginAttempt
From #TmpErrorLog
Where LogDate > GETDATE() - 120 and 
[ProcessInfo] = 'Logon' and 
Text like '%Login%' and text not like '%untrusted%'



INSERT INTO [MyTABLE] ([LogDate], [LoginAttempt], [LoginUser], [ServerName], [Log_date_key])
Select 
LogDate, 
Case LoginAttempt When 'Login succeeded' Then 'Successful' ELSE  'Failed' End as    LoginAttempt,
SUBSTRING(SUBSTRING(Text, CHARINDEX('''', Text,1), CHARINDEX('''', Text, 0) - 4),2,50)    as LoginUser, @@SERVERNAME, (Convert(INT, LogDate, 112))
From #TmpErrorLog2
Where LogDate Not In(Select LogDate From [MyTable]) 



Drop Table #TmpErrorLog
Drop Table #TmpErrorLog2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:27:17+00:00Added an answer on June 19, 2026 at 3:27 am

    Your substring usage is a little off in the final select.

    You have

    SUBSTRING(SUBSTRING(Text, CHARINDEX('''', Text,1), CHARINDEX('''', Text, 0) - 4),2,50)
    

    where what you need is

    SUBSTRING(
        Text, 
        CHARINDEX('''', Text,1)+1, 
        CHARINDEX('''', Text, CHARINDEX('''', Text,1)+1) - CHARINDEX('''', Text,1)-1
    )
    

    You were providing a start location and an end location, tsql requires the starting location and the length of string you wish returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to track down a problem with a query I have.
I'm trying to track down why a db query got screwed in SQL Server
I am trying to track down all stored procedures in a database that have
Good Day, I have a sql query that returns the following data: EmployeeID...Employee 555..........John
Does SQL Services Reporting Services have a log file that keeps track of changes
I'm trying to use SQL Server Profiler (2005) to track down some application performance
I'm trying to track podcast data. They told me I have to use a
I'm trying to track down an issue in our MFC code that looks like
I'm trying to track down a bug that occurs when I click a particular
I'm trying to track the error thrown by SQL Server 2005. The problem is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.