5

Is it possible to capture the regex match in the select part of the query in mysql?

I'd like to query for an initial letters in the UK postcode like:

SELECT all initiall letters from a post code (one or two) FROM addresses;

UK postcodes start with one or two letters and then have one or two digits, optional space, then two letters and finally a digit.

Examples:

SW8 4EX

E1 7AG

EC1 8AG

SE17 9AW

5
  • What are you trying to achieve? Please provide examples. Perhaps, have a look at What is the correct syntax for a Regex find-and-replace using REGEXP_REPLACE in MariaDB?. Commented Sep 11, 2015 at 10:07
  • this post might help stackoverflow.com/questions/21378193/… Commented Sep 11, 2015 at 12:55
  • @pcantalupo that post is about Sql Server ... Commented Sep 14, 2015 at 9:13
  • What's the content of the addresses column? can you post some sample data? It might be just a SUBSTRING(addresses, 0, 2). Commented Jun 12, 2018 at 22:18
  • @solarc I've added some examples of UK postcodes Commented Jun 13, 2018 at 10:18

1 Answer 1

1
SELECT REGEXP_REPLACE('SW8 4EX', ' .*', '');

This should work with your examples. First make sure the postcode column does not contain leading space.

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

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.