Solved

Remove special characters

Posted on 2016-09-06
5
54 Views
Last Modified: 2016-09-10
Greetings!

I would like some assistance to remove the | sign ONLY, if the Category IDs start with |

Example: Per highlighted screenshot, I would like to remove the | in the front of the column. I would like to leave the | for the rest to help separate the numbers. Thanks!

 remove pipeline
0
Comment
Question by:Member_2_7967487
5 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41787313
try like:
select *, 
case when charindex('|',CategoryIDs)=1 then substring(CategoryIDs,charindex('|',CategoryIDs)+1,len(CategoryIDs)-1) else CategoryIDs end New_CategoryIDs from Items

Open in new window

or
select *, 
case when charindex('|',CategoryIDs)=1 then substring(CategoryIDs,2,len(CategoryIDs)-1) else CategoryIDs end New_CategoryIDs from Items

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41787370
I think using left is more effective:
select *, case when left(CategoryIDs,1) <> '|' then CategoryIDs else substring(CategoryIDs,2,8000) end CategoryIDs2 from Items

Open in new window

0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 41787379
Please test this in a DEV environment before running in Production:
UPDATE Items
SET CategoryIDs = SUBSTRING (CategoryIDs ,2, LEN(CategoryIDs)-1)  
WHERE LEFT(CategoryIDs,1) = '|'

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41788000
You should use LIKE, because it is "sargable", rather using any function on the column, which is not sargable.  Thus, indexes could be far more efficiently used with LIKE.

I've also found STUFF to be more efficient than SUBSTRING to remove a char(s).

Therefore, my code would be:

UPDATE ...
SET CategoryIDs = STUFF(CategoryIDs, 1, 1, '')
WHERE CategoryIDs LIKE '|%'
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL date incremented 11 24
How can i use WITH CTE for checking exist value? 3 30
migrate a SQL 2008 to 2016, 2 26
SQL Server 2012 - Merge Replication Issue 1 19
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question