Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split comma delimited string --> FUNCTION db.CHARINDEX does not exist

I need to split comma delimited string into a second columns I have the following table :

CL1     POS                 POS2     LENGHT     ALLELE
1       3015108,3015109              5          A
2       3015110,3015200              10         B
3       3015200,3015300              15         C
4       3015450,3015500              20         D
5       3015600,3015700              15         E

I want to split the numbers after the comma into a second column POS2 So it should like that

CL1     POS                 POS2     LENGHT     ALLELE
1       3015108             3015109  5          A
2       3015110             3015200  10         B
3       3015200             3015300  15         C
4       3015450             3015500  20         D
5       3015600             3015700  15         E

So I've queried the following :

INSERT INTO MyTable (POS2)
SELECT RIGHT(POS, CHARINDEX(',', POS) + 1 ) FROM MyTable ;


 It returns an error : 
 ERROR 1305 (42000): FUNCTION test.CHARINDEX does not exist
like image 723
madkitty Avatar asked Mar 31 '12 04:03

madkitty


People also ask

Why Char (0) is not included in charindex?

0x0000 ( char (0)) is an undefined character in Windows collations and cannot be included in CHARINDEX. When using SC collations, both start_location and the return value count surrogate pairs as one character, not two.

Is there a charindex () function in MySQL?

Bookmark this question. Show activity on this post. CL1 POS POS2 LENGHT ALLELE 1 3015108,3015109 5 A 2 3015110,3015200 10 B 3 3015200,3015300 15 C 4 3015450,3015500 20 D 5 3015600,3015700 15 E Show activity on this post. MySQL doesn't have a built-in CHARINDEX () function.

Can charindex be used with image data type?

CHARINDEX cannot be used with image, ntext, or text data types. If either the expressionToFind or expressionToSearch expression has a NULL value, CHARINDEX returns . If CHARINDEX does not find expressionToFind within expressionToSearch, CHARINDEX returns 0. CHARINDEX performs comparisons based on the input collation.


2 Answers

MySQL doesn't have a built-in CHARINDEX() function. LOCATE() would be the MySQL equivalent.

Using SUBSTRING_INDEX() might be a more succinct way of doing this. Something like this (disclaimer: untested):

SUBSTRING_INDEX(POS, ',', 1) for POS

SUBSTRING_INDEX(POS, ',', -1) for POS2


As an aside, I may be misunderstanding what you're trying to accomplish, but it looks like you might want to UPDATE existing rows, not INSERT new ones? Something like:

UPDATE MyTable SET POS2 = SUBSTRING_INDEX(POS, ',', -1); UPDATE MyTable SET POS = SUBSTRING_INDEX(POS, ',', 1); 
like image 81
Wiseguy Avatar answered Sep 21 '22 07:09

Wiseguy


MySQL does have a similar function: InStr or for the same syntax Locate.

like image 44
bill Avatar answered Sep 19 '22 07:09

bill