1

I need to take a postal code looking like:

S7Y 6H5

that's in a table and display it as

S7Y & 6H5

I can't find a command that splits the string in sql.

4
  • 1
    Why do you have to do this in SQL? Can't you just do it once you've retrieved the information? Not saying your approach is wrong, but just wondering if this is really necessary. Commented Mar 11, 2013 at 19:51
  • Is it only ever a single space, and you you need the literal 'S7Y & 6H5', or do you want them in 2 seperate fields/columns? Commented Mar 11, 2013 at 19:51
  • Is substring_index() what you are looking for? Commented Mar 11, 2013 at 19:54
  • also you should be able to use SUBSTR along with concatenation to make all kinds of variants. Commented Mar 11, 2013 at 19:57

3 Answers 3

3

MySQL doesn't include a split function, but it does include a replace. You could use:

SELECT REPLACE('S7Y 6HS', ' ', ' & ')

The greater question for me, though, is why don't you do that in your application code?

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

Comments

2

Try this:

Select REPLACE ('S7Y 6H5', ' ', ' & ')

Comments

1

you can use INSERT INSERT(str,pos,len,newstr)

     SELECT INSERT('S7Y 6H5', 4, 0, ' & ');

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_insert

Comments

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.