Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters from text using TRANSLATE function (replace them with empty strings)

Let's say I have a string:

DECLARE @text VARCHAR(20) = 'abr_akad-ab#ra';

and I want to remove all _-# characters from the text.

Normally I would user REPLACE function to that, something like:

SELECT REPLACE(REPLACE(REPLACE(@text, '-', ''), '_', ''),'#','')

Can I do that with single TRANSLATE statement somehow?

like image 833
Dmitrij Kultasev Avatar asked Aug 31 '25 10:08

Dmitrij Kultasev


1 Answers

You can try the following query:

DECLARE @text AS VARCHAR(20) = 'abr_akad-ab#ra';
SELECT REPLACE(TRANSLATE(@text, '_-#', '###'), '#', '')

it will return the output as abrakadabra

Working demo on db<>fiddle

like image 65
Arulkumar Avatar answered Sep 03 '25 23:09

Arulkumar