-4

I have a column let's say note and the values inside the notes is like:

Medicine. This needs to be use with the right prescription. wwww.test.comURL:www.test.com/medicine/test.php

I want to extract or get the result of those in 3 parts:

  1. Medicine
  2. This needs to be use with the right prescription.
  3. www.test.com/medicine/test.php

But, how can I do that using a select statement in MySQL? Any help is highly and really appreciated!

The Impaler
  • 45,731
  • 9
  • 39
  • 76
Database Admin
  • 83
  • 2
  • 10
  • 2
    I think your question needs to be better structured with example data included. What exactly are you asking? You have a column in a table which is populated with a chunk of text and you want to write a query which parses that text into three columns? – Barry Piccinni Aug 07 '18 at 13:28
  • @BarryPiccinni thanks and yes Exactly that's what I want! For example the data inside the notes column is: Medicine. This needs to be use with the right prescription. wwww.test.comURL:www.test.com/medicine/test.php ----- I want to parse it on a result like 1. Medicine 2. This needs to be use with the right prescription. 3. www.test.com/medicine/test.php And put it on another table on a 3 columns Types, Description, URL where types = medicine, description = This needs to be use with the right prescription. and URL = www.test.com/medicine/test.php using MySQL query or SELECT statement. – Database Admin Aug 07 '18 at 14:13

1 Answers1

1

Well, the simple answer is: Not easily. You can write a function which will do this for you.

You should read the following solution: Split value from one field to two

Taken from the above answer:

With that function:

DELIMITER $$

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255) DETERMINISTIC
BEGIN 
    RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');
END$$

DELIMITER ;

you would be able to build your query as follows:

SELECT SPLIT_STR(membername, ' ', 1) as memberfirst,
       SPLIT_STR(membername, ' ', 2) as memberlast
FROM   users;

If you improve your question, I can give you a more specific answer on how to implement this, but you have provided little information about the structure of this column or how it changes from row to row.

EDIT:

A bit of the usage of this function: It takes in 3 parameters.

  1. The string you want to split up.

  2. A delimiter. That is, one or more character(s) you want to split your string on.

  3. An index. Use this to select which part of the split up string you want.

So for example if your column was formatted as such:

SELECT summary
FROM MyTable

Result:

          summary
----------------------------
 "foo. bar. www.foobar.com"

You could put something like:

SELECT SPLIT_STR(summary, '. ', 1) AS a_summary,
       SPLIT_STR(summary, '. ', 2) AS a_description,
       SPLIT_STR(summary, '. ', 3) AS a_url,
FROM MyTable

An output might look something like:

a_summary | a_description |    a_url
------------------------------------------
   foo    |     bar       | www.foobar.com
Barry Piccinni
  • 1,685
  • 12
  • 23
  • Thank you this works perfectly! by the way, I need to split it to three parts. Should I add a third column inside the function which is on varchar data type as well? – Database Admin Aug 08 '18 at 10:52
  • @DatabaseAdmin Im not sure how your table will vary from row to row, so I can't really be more specific than what I've posted, but broadly speaking no, you shouldn't need to change the function, you should only need to amend the query. So in the query above, you should have three "SPLIT_STR() as" statements rather than two. – Barry Piccinni Aug 08 '18 at 11:03
  • my data is like this: Regular holiday. Commemorates the execution of José\; Rizal by the Spanish on 30 Dec 1896Information provided by www.officeholidays.comURL:https://www.officeholidays.com/countries/philippines/rizal_day.php and when I run the split query SELECT SPLIT_STR(Summary, '.', 1) as HolidayType, SPLIT_STR(Summary, 'Information provided by www.officeholidays.comURL:', 2) as URL FROM holidaynobusiness; I got right result for 2 columns : Regular holiday | https://www.officeholidays.com/countries/global/christmas_day.php but I also need the Commemorates up to 1986 – Database Admin Aug 08 '18 at 12:07
  • so that will be 3 new columns Type, Description/Summary and URL column. From the function that I used, I got the right value for Type and URL, but as I've mentioned I also need to get the Commemorates the execution of José\; Rizal by the Spanish on 30 Dec 1896 for the Description column – Database Admin Aug 08 '18 at 12:10
  • @DatabaseAdmin You're not using it quite as intended. Have a look at my edit. – Barry Piccinni Aug 08 '18 at 13:08