简体   繁体   中英

Substring SQL With a condition

I must extract a sub-string but only if a condition is met. The users in this column must fill the telephone numbers of clients (It's a varchar column),

Some examples of those stored values are:

  1. 23==880-3112==9435
  2. 52==031 31466==171
  3. 321==15850

The '=' are numbers I don't wanna share.

I need to extract the mobile number (The one with the length of 10), but as you can see, those numbers are not stored in the same positions, I can't use a LEFT() or RIGHT() function because of that, some are separated with '-', others with spaces between the house number and mobile number.

If this can't be done with SQL, I'm using Java but I don't even know where to start.

Thanks in advance.

EDIT: I'm using SQL SERVER 2012 The expected results from the examples are

  1. 3112==9435
  2. 31466==171
  3. 312==15850

I want to obtain always the 10 characters number.

Sorry and thanks

You can use String comparison function

Where part of your 1st example will be

WHERE TELEPHONE_NO LIKE '23%%880-3112%%9435'

[Edit]

To extract a substring you can use https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index

mysql> SELECT SUBSTRING_INDEX('23==880-3112==9435', '-', 10);
    -> '3112==9435'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM