How can I split up a name before/after the and insert into a new table with other attributes that both artists have?
I prefer to split and keep in same table.My Artists
table contains Artist
, PageURL
, AlbumURL
.
My table results:
How can I split up a name before/after the and insert into a new table with other attributes that both artists have?
I prefer to split and keep in same table.My Artists
table contains Artist
, PageURL
, AlbumURL
.
My table results:
You can use the following split up query to get name seperated :
SELECT SUBSTRING_INDEX(Artist,' And ',1) as name1,
SUBSTRING_INDEX(Artist,' And ',-1) as name2 ,PageURL,AlbumURL
From Artists
Add this select statement into an insert query for the new table
INSERT INTO Artists2
SELECT SUBSTRING_INDEX(Artist,' And ',1) as name1,
SUBSTRING_INDEX(Artist,' And ',-1) as name2 ,PageURL,AlbumURL
From Artists
This may help...
DECLARE @Vlaue VARCHAR(100) = 'ABCD and EFGH'
DECLARE @index INT = (SELECT CHARINDEX('and', @Vlaue))
SELECT SUBSTRING(@Vlaue,1,@index-1) as FirstPart, SUBSTRING(@Vlaue,@index+3,LEN(@Vlaue) - @index) SecondPart
--SELECT SUBSTRING(@Vlaue,1,CHARINDEX('and', @Vlaue)-1) as FirstPart, SUBSTRING(@Vlaue,CHARINDEX('and', @Vlaue)+3,LEN(@Vlaue) - @index) SecondPart
Below query will split two names but if you have more than two names in Artist column then you have to create MySQL function.
select
*,
substring_index(Artist, ' And ', 1) as FirstArtist,
substring_index(Artist, ' And ', -1) as LastArtist
from Artists;
Check this post- Split value from one field to two
Do you want below stuff? please confirm.
Assume current data -
Artist
Metz And Trix
Djo2 And DJ Srk
Required data in same table -
Artist
Metz
Trix
Djo2
DJ Srk
Solution- You need to insert second artists then update the first artists records. Please execute insert command first then update command. Please test before applying to production.
insert into Artists (Artist, PageURL, AlbumURL)
select substring_index(Artist, ' And ', -1), PageURL, AlbumURL
from tmp.Artists;
update Artists
set Artist = substring_index(Artist, ' And ', 1)
where Artist like '% And %'