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 9267793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:38:23+00:00 2026-06-18T14:38:23+00:00

My company has a logging table containing a VARCHAR(N) column in which a string

  • 0

My company has a logging table containing a VARCHAR(N) column in which a string is placed that is supposed to be XML, but as it turns out it is not always well-formed. In order to perform analysis on the logging (to identify error trends, etc.), I have been using a LIKE statement. However, this is remarkably slow.

Recently, I discovered that SQL Server supports XQuery, so I started playing with it. The problem I’m running into is that I can’t figure out a way to handle CAST/CONVERT errors within my SELECT statement. The closest I’ve come requires SQL Server 2012 as it has the TRY_CONVERT function, but upgrading from 2008 R2 is not an option right now.

Here is what I have (which would work if my company was running 2012):

CREATE FUNCTION IsMatch(
    @message AS VARCHAR(MAX),
    @match AS VARCHAR(MAX),
    @default AS VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @xml XML = TRY_CONVERT(XML, @message)
    DECLARE @result VARCHAR(MAX) =
        CASE WHEN @xml IS NOT NULL
             THEN CASE WHEN @xml.exist('(/FormattedMessage)[contains(.,sql:variable("@match"))]') = 1
                       THEN @match 
                       ELSE @default 
                       END
             ELSE CASE WHEN @message LIKE '%' + @match + '%'
                       THEN @match
                       ELSE @default
                       END
             END
    RETURN @result
END

GO

DECLARE @search VARCHAR(MAX) = 'a substring of my xml error message'

SELECT Error, COUNT(*) as 'Count'
FROM ( SELECT TOP 319 [LogID]
          ,[Severity]
          ,[Title]
          ,[Timestamp]
          ,[MachineName]
          ,[FormattedMessage]
          --,CAST([formattedmessage] as xml)
          ,IsMatch(@search, 'Other') as 'Error'
       FROM [MyDatabase].[dbo].[Log] (NOLOCK) ) a
GROUP BY Error

The commented CAST (or alternately a CONVERT) will cause the query to error as soon as it encounters malformed XML. If I limit to a TOP (N), I can ensure that there are no errors and the SELECT statement work incredibly fast. I just need a way to handle errors on a per-row basis.

I considered using a TRY/CATCH in IsMatch(), but that can’t be used in a function. Alternatively, to use the TRY/CATCH, I considered a stored proc, but I can’t figure out how to include that in my SELECT clause.

  • 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-18T14:38:25+00:00Added an answer on June 18, 2026 at 2:38 pm

    If you’re stuck on 2008 R2 I think what you want to do is use a readonly forward CURSOR in your stored procedure. Then use the TRY CATCH block inside of the WHILE @@FETCH_STATUS = 0 loop.

    DECLARE logcursor CURSOR FORWARD_ONLY READ_ONLY FOR 
    SELECT TOP 319 [LogId]
              ,[formattedmessage]
           FROM [GenesisLogging].[dbo].[Log] (NOLOCK)
    
    OPEN logcursor
    
    FETCH NEXT FROM logcursor
    INTO @id, @formattedmessage
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        BEGIN TRY
        SET @xml = CONVERT(xml, @formattedmessage)
        END TRY
        BEGIN CATCH
        PRINT @id
        END CATCH; 
        FETCH NEXT FROM logcursor 
        INTO @id, @formattedmessage
    END 
    CLOSE logcursor;
    DEALLOCATE logcursor;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My company has been using XML-RPC for a while, but lately I'm wondering what
My company has given me a PDF that's already been mastered, packaged, etc. but
My company has got a new project upcoming and i need software that can
Our company has a product which relies on local database to work (it allows
My company has a problem: we suspect that the NACHA files we are receiving
My company has built a machine that measures wedge and roundness of lenses by
My company has a handful of apps that we deploy in the websites we
My company has a ClickOnce application that has been in use with our customers
My company has an Enterprise client/server application that runs on Android. I am adding
I've been working with a company that, in this current project, has to implement

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.