LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   MySQL splitting a string with a delimiter and taking the 1st value and update (https://www.linuxquestions.org/questions/programming-9/mysql-splitting-a-string-with-a-delimiter-and-taking-the-1st-value-and-update-617710/)

Lantzvillian 01-31-2008 12:19 PM

MySQL splitting a string with a delimiter and taking the 1st value and update
 
Hey everyone,

I am trying to take a string like "Brown, James" sans quotes and taking the substring James and tossing it into the column first_name.

Here is what I think would do it, but I know its not the right way because it doesn't work.

insert into instructors (first_name) SELECT SUBSTRING_INDEX(instructor, ' , ', 1) FROM srs_data;


Any help would be great,

Ronnie

shakezilla 01-31-2008 12:38 PM

I noticed in your code you actually have:
Code:

' , '
with space before and after comma. Could just be the way you typed it here, but don't do that.

Can you post your mysql output?

Lantzvillian 01-31-2008 12:46 PM

mmmh yeah accident.

+---------------+--------------+-----------+---------------+----------------+---------------+
| instructor_id | first_name | last_name | office_number | office_phone | instructor |
+---------------+--------------+-----------+---------------+----------------+---------------+
| 1 | NULL | NULL | Office Num | OffiicePhone | Instructor |
| 2 | NULL | NULL | 180/222 | (205) 740-7661 | Avery, Terry |
| 3 | NULL | NULL | 180/226 | (205) 740-6116 | Dutchuk, Mark |
| 4 | NULL | NULL | 180/227 | (205) 740-6777 | Croft, David |
| 5 | NULL | NULL | 180/226 | (205) 740-6777 | Croft, David |
| 6 | NULL | NULL | 180/224 | (205) 740-6777 | Croft, David |
| 7 | NULL | NULL | 180/224 | (205) 740-6778 | Loewen, John |
| 17 | Avery, Terry | NULL | NULL | NULL | NULL |
| 16 | Avery, Terry | NULL | NULL | NULL | NULL |
| 15 | Avery, Terry | NULL | NULL | NULL | NULL |
+---------------+--------------+-----------+---------------+----------------+---------------+

Instructor_id 1-7 were there before as a test. 17 + are the result of that statement.

Lantzvillian 01-31-2008 12:53 PM

Ok it will do the last_name like I want now, however I want it to update the table: I get a syntax error, so I think I am being too clever for MySQL.. php you can get away with murder ;)

update instuctors WHERE last_name = SELECT DISTINCT SUBSTRING_INDEX(instructor, ', ', 1) FROM instructors;

Lantzvillian 01-31-2008 02:05 PM

Fixed part of it:
mysql> insert into instructors (last_name) SELECT SUBSTRING_INDEX(instructor, ', ', 1) FROM instructors;
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from instructors;
+---------------+------------+------------+---------------+----------------+---------------+
| instructor_id | first_name | last_name | office_number | office_phone | instructor |
+---------------+------------+------------+---------------+----------------+---------------+
| 1 | NULL | NULL | Office Num | OffiicePhone | Instructor |
| 2 | NULL | NULL | 180/222 | (205) 740-7661 | Avery, Terry |
| 3 | NULL | NULL | 180/226 | (205) 740-6116 | Dutchuk, Mark |
| 4 | NULL | NULL | 180/227 | (205) 740-6777 | Croft, David |
| 5 | NULL | NULL | 180/226 | (205) 740-6777 | Croft, David |
| 6 | NULL | NULL | 180/224 | (205) 740-6777 | Croft, David |
| 7 | NULL | NULL | 180/224 | (205) 740-6778 | Loewen, John |
| 331 | NULL | Loewen | NULL | NULL | NULL |
| 330 | NULL | Croft | NULL | NULL | NULL |
| 329 | NULL | Croft | NULL | NULL | NULL |
| 328 | NULL | Croft | NULL | NULL | NULL |
| 327 | NULL | Dutchuk | NULL | NULL | NULL |
| 326 | NULL | Avery | NULL | NULL | NULL |
| 325 | NULL | Instructor | NULL | NULL | NULL |
+---------------+------------+------------+---------------+----------------+---------------+
14 rows in set (0.00 sec)

Its still not doing what I want though, any suggestions?

shakezilla 01-31-2008 03:16 PM

1st, maybe wanna think about posting actual phone numbers on the net. This page will be archived and be around forever, maybe use some asterisks.

2nd, I have a suggestion, but make a copy of the data and work on that. I haven't used mysql in a long time, and I have no idea if this will work. What you're doing there is adding new rows, when it sounds like you need to be updating rows.

Try:
Code:

UPDATE instructors SET last_name=SUBSTRING_INDEX(instructor, ", ", 1), first_name=SUBSTRING_INDEX(instructor, ", ", -1)
WHERE instructor IS NOT NULL;


Edit
----
Also use code tags.

Lantzvillian 01-31-2008 11:18 PM

Quote:

Originally Posted by shakezilla (Post 3041514)
1st, maybe wanna think about posting actual phone numbers on the net. This page will be archived and be around forever, maybe use some asterisks.

2nd, I have a suggestion, but make a copy of the data and work on that. I haven't used mysql in a long time, and I have no idea if this will work. What you're doing there is adding new rows, when it sounds like you need to be updating rows.

Try:
Code:

UPDATE instructors SET last_name=SUBSTRING_INDEX(instructor, ", ", 1), first_name=SUBSTRING_INDEX(instructor, ", ", -1)
WHERE instructor IS NOT NULL;


Edit
----
Also use code tags.

No worries, they aren't real anyways, you'll wind up in alabama ;) Thanks for the help though. Awesome actually, nice and clean.

So one more quick question. I would trim the instructor column to get rid of the ", " and then to add a "@hotmail.com" I would use concatonation?

Lantzvillian 01-31-2008 11:57 PM

Code:

UPDATE students SET email = LOWER(CONCAT(LEFT(SUBSTRING_INDEX(first_name, ", ", 1),1),SUBSTRING_INDEX(last_name, ", ", -1),"@shoes.ca")) WHERE first_name IS NOT NULL;
Gotter :) Made it lowercase, shifted letters and concatenated on the email prefix. :) Thanks Man


All times are GMT -5. The time now is 02:18 PM.