0

How to pick error message from this error string:

[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)

I just need to give the error:

Conversion failed when converting from a character string to uniqueidentifier.

to the user. How do i do it?

0

3 Answers 3

4

You can use something like

s = "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)"
print(s[s.rfind(']')+1: s.find('(')])
Sign up to request clarification or add additional context in comments.

1 Comment

works if the error is isolated. does not work if the error is contained more then once in a string (aka finding error inside a log file): bla [something]text1(bla)\nbla [some other thing]text2(bla)\n gives you an empty slice
2

If you want to find multiple occurences of this pattern in a longer text, @mohammedwazeems's solution no longer works.

In that case, use regex:

import re

regex = r".*](.+?)[(]"   # avoid all until last ] that is followed by captured anything lazyly
                         # that is followed by an open (

log_file = """some text that is fine
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)
more ok text
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Fix me error. (8169) (SQLExecDirectW)
more okish text"""

matches = re.finditer(regex, log_file, re.MULTILINE)

for match in matches:

    if len(match.groups())>0:
        print ( match.group(1))

prints :

Conversion failed when converting from a character string to uniqueidentifier. 
Fix me error.

Test it here: https://regex101.com/r/GPWs2a/1

2 Comments

Perhaps it's easier to use something like this: error[error.rfind(']')+1:error.find('(')]
@ShadyMBA - if you look for that occurence in text that contains it exactly ONCE - yes. Try finding two to hundred occurences of that in one longer log file ... and you can't. Regex can. In fact stackoverflow.com/a/60826166/7505395 will get you NOTHING if you feed it text that contains the error message twice because the slice will look something like [99:43] (i.e. the first ] from back and first ( from back will target two different messages alltogether and get an empty slice.
0

Try this:

error = '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)'
error[error.rfind(']')+1:error.find('(')]

That should give:

'Conversion failed when converting from a character string to uniqueidentifier. '

2 Comments

Its not perticular message, it could me aything for example [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]PickMe. (8169) (SQLExecDirectW)
he said " this error string" so he's very specific. If he needs it to be more generic then he needs to specify so in his question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.