In customers table I have Email column which could contain multiple emails separated by (;).
I used split function to separate emails for each customer:
Cust1 --->email1
cust1 --->email2
cust1 ---> emailN
And I could add more emails to the same customer.
I want to be able to update or delete the splitted emails, in other words if email2= abc@company.com I want to change it to xyz@company.com or delete it.
Is it possible to do using split function? or any other way?
Here is my split function
CREATE FUNCTION [dbo].[fnSplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
Calling the function to split emails:
select tb1.custId, split.splitdata from customers tb1
outer apply [dbo].[fnSplitString] (tb1.email,';') split
where tb1.Email like '%;%'
To add new email to the same customer:
UPDATE Customers set Email=Email+';new Email' Where CustId='customerId'
for updating or deleting existing emails, any suggestions?
Thanks in advance
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire