Search code examples
mysqlsqlpositionsubstring

How can I specify the position of a given symbol in a substring function?


Let's suppose I have this column with this kind of data:

IT > FR
ES > PT 
FR > IT 

And I want to show two separate columns, where the results are: in the first, only the country code of the first country and, in the second, only the ones of the second.

We use the substring function. And it's okay.

Now, let's assume I have this column called border:

IT > FR
ES > PT 
FR > IT
DE > PORTO
ES > NL
NO2 > UK

The length of the borders are not the same!

Is there a way to specify in the substring function that I want for a column only the string from the position of > backwards or onwards?


Solution

  • https://www.w3resource.com/mysql/string-functions/mysql-locate-function.php

    Locate([your column],'>',1)
    

    Or am I missing something?

    Left([Your column],Locate([your column],'>',1)-1) --To grab the left side
    Right([Your column],Locate([your column],'>',1)+1) --to grab the right side