You desperately need to fix the schema. Storing multiple values in a string is just wrong. But sometimes we are stuck with other people's really, really, really bad decisions.
I would suggest this structure:
UPDATE table SET shareWith = TRIM(',' FROM REPLACE(CONCAT(',', ShareWIth, ','), CONCAT(',', ?, ',')) ) WHERE reportName = ? AND CONCAT(',', shareWith, ',') LIKE CONCAT('%,', ?, ',%');
That is, simplify the logic by putting delimiters around the searched string and the pattern.
However, I will repeat: you should have a table with one value per report and share.
Instead of the more flexible
TRIM(), you can use spaces and
SET shareWith = REPLACE(TRIM(REPLACE(REPLACE(CONCAT(',', ShareWIth, ','), CONCAT(',', ?, ','))), ',', ' '), ' ', ',')
This assumes that the string doesn't have spaces, which makes sense for emails in a string.
Please try the following solution.
It will work starting from SQL Server 2012 onwards.
It is using XML and XQuery to tokenize email list, and remove not needed email. You can package it as a stored procedure with two parameters, @reportName and @emailToRemove.
The CTE does all the heavy lifting:
-- DDL and sample data population, start DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, reportName VARCHAR(30), ShareWith VARCHAR(1024)); INSERT INTO @tbl (reportName, ShareWith) VALUES ('IBM SH data', '[email protected],[email protected],[email protected]'), ('Samsung Sr', '[email protected],[email protected]'), ('Xiaomi MFG', ''), ('Apple US st', '[email protected],[email protected]'), ('LG EU', '[email protected]'); -- DDL and sample data population, end DECLARE @reportName VARCHAR(30) = 'IBM SH data' , @emailToRemove varchar(30) = '[email protected]' , @separator CHAR(1) = ','; ;WITH rs AS ( SELECT * , REPLACE(TRY_CAST('<root><r><![CDATA[' + REPLACE(ShareWith, @separator, ']]></r><r><![CDATA[') + ']]></r></root>' AS XML) .query(' for $x in /root/r[lower-case((./text())) ne lower-case(sql:variable("@emailToRemove"))] return data($x) ').value('.', 'VARCHAR(MAX)'), SPACE(1), @separator) AS modifiedShareWith FROM @tbl WHERE reportName = @reportName ) UPDATE t SET ShareWith = rs.modifiedShareWith FROM @tbl AS t INNER JOIN rs ON t.id = rs.id; -- test SELECT * FROM @tbl;
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.