Here is a setup to demonstrate the problem:
create table foo (
[goat] [varchar](20) NOT NULL,
[email] [varchar](max) NULL,
[id] [int] NOT NULL
)
GO
INSERT INTO foo (
[goat],
[email],
[id]
)
values
('bluegoat', 'hello@goat.com', 1),
('bluegoat_addl_emails', 'goats@goat.com,cow@goat.com,wolf@goat.com,dog@goat.com,kitten4235@goat.com',1)
GO
select * from foo
GO --to test the setup, it should look like this:
goat email id
bluegoat hello@goat.com 1
bluegoat_addl_emails goats@goat.com,cow@goat.com,wolf@goat.com,dog@goat.com,kitten4235@goat.com 1
/*here comes the fun part*/
with independent(goat, email, emailITEM, id) as
(
SELECT [goat]
,left(email, CHARINDEX(',',email+',')-1)
,STUFF(email, 1, CHARINDEX(',',email+','), '')
,[id]
FROM foo
union all
SELECT [goat]
,left(emailITEM, CHARINDEX(',',email+',')-1)
,STUFF(emailITEM, 1, CHARINDEX(',',email+','), '')
,[id]
FROM independent
where
emailITEM > ''
)
select * from independent
order by id
Using a modified version of this: Turning a Comma Separated string into individual rows I've attempted to break up the comma separated field. The original solution actually doesn't work with more than two CSV's, so I had to modify it to go through the field as many times as necessary.
goat email emailITEM id
bluegoat hello@goat.com 1
bluegoat_addl_emails goats@goat.com cow@goat.com,wolf@goat.com,dog@goat.com,kitten4235@goat.com 1
bluegoat_addl_emails cow@goat.com, olf@goat.com,dog@goat.com,kitten4235@goat.com 1
bluegoat_addl_emails olf@goat.com, og@goat.com,kitten4235@goat.com 1
bluegoat_addl_emails og@goat.com, itten4235@goat.com 1
bluegoat_addl_emails itten4235@g at.com 1
bluegoat_addl_emails at.com 1
As you can see, it's supposed to start a new result at each comma, but the cursor keeps moving to the right, chopping off one additional character at the end of each line.
I know I'm close to the right solution and I can tell it's something with the numbers, but I can't seem to adjust the steps so that it stops breaking up the text at random points of the string.
Chances are, someone will look at this and immediately see the solution contrary to my exhausted brain, so I thought I'd give it a try here. Thank you so much to/if someone can give a hint.
Update: to adhere to the question format of S/o: How can I modify this function to make sure the string is chopped up right at each comma, discarding the commas and keeping the email addresses?