1

I got an sql question. I have a table containing a column named title that store a string like this in all raws.

"Prenom - Nom (85)".

I would like to know if there is a way in sql to change that string like this :

"Nom - Prenom" 

It means, I would like to reverse it and then delete the " (85).

Thank you in advance.

2
  • you want to delete the last four charachters? and also "-" would always be there? Commented Jan 31, 2014 at 11:38
  • delete it and then reverse the first string si as i get nom - prenom Commented Feb 28, 2014 at 16:51

2 Answers 2

2

Yes:

select concat(substring_index(left(col, length(col) - instr(reverse(col), ' ')), ' - ', -1),
              ' - ',
              substring_index(col, ' - ', 1)
             )

This assume that you want something a bit more general than getting rid of the '(85)'; it removes the final word.

Sign up to request clarification or add additional context in comments.

Comments

0

Ugly as hell and almost certainly won't work for all of your cases. But this works for the example you have posted:

select concat(
substring_index(substring_index("Prenom - Nom (85)"," (",1)," - ",-1),
" - ",
substring_index(substring_index("Prenom - Nom (85)"," (",1)," - ",1)
);

1 Comment

Am goint to test this and give feedback.

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.