Query to return the value that comes before a specific character

Asked

Viewed 38 times

1

I have a table called answer which is filled with answers to certain questions, each answer usually begins with the letter that would be the answer, for example: a)..., b)..., c)... ou d)..., the problem is that it doesn’t always start like this, it can happen to start with a code html to then come the letter of reply, can come like this <p>a)...

I would like a query that returns only the letter that comes before closing parentheses ), remembering also that you should take only the value that precedes the first parentheses, in case the answer may appear other parentheses.

I’m wearing it like this:

Select substr(answer,1,1) AS resposta

But it’s no good if something else comes before the letter of reply.

  • I think the ideal would be to store the alternative in another column, would facilitate, but... take a look at the POSITION https://www.w3resource.com/mysql/string-functions/mysql-position-function.php

  • @Rovannlinhalis this is true, this database is from Moodle, I’ve worked and there is no table with these answers, but only with the full answer, these letters I add to generate a feedback, it was the only way I found

  • If you know regular expression, you can use a function in Mysql: https://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql

1 answer

2


Follows the idea commented by Rovann, uses the position():

SELECT substr (coluna_answer, (POSITION(")" IN coluna_answer) - 1), 1) THE answers

  • Testing here...

  • 1

    It worked perfectly! Thanks rLinhares.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.