0

I'm looking for a select statement in which I can refine values in a nvarchar field. For example: the input value is '!ab>c2 ghf5 ksj@_+wwl9' and output must be 'abc ghf ksjwwl'. How can I remove some extra characters within a text with SQL regex or other ways?

Hossein POURAKBAR
  • 1,073
  • 2
  • 15
  • 33
  • Have those extra characters located before space? – Viki888 Nov 30 '16 at 09:32
  • 1
    What exactly is it you want to acheive? Do you want to do a select and trim certain characters from every column or a specific column in the resultset, or do you want to remove the characters from your input (in a stored procedure for example) and then do a search using your trimmed search string? – Jerri Kangasniemi Nov 30 '16 at 09:34
  • @Viki888 I want to update one of the fields permanently and I don't know anything about which extra characters are. So I just need to keep letter characters and remove the others. – Hossein POURAKBAR Nov 30 '16 at 10:28

1 Answers1

0

I don't know if this helps, but you can define a table with all characters / phrases you want to replace and do it like this:

DECLARE @CharsToReplace TABLE(SearchFor VARCHAR(100),ReplaceWith VARCHAR(100));
INSERT INTO @CharsToReplace VALUES('1',''),('2',''),('3',''),('4',''),('5',''),('6',''),('7',''),('8',''),('9',''),('0','');

DECLARE @YourString VARCHAR(100)='abc2 ghf5 ksjwwl9';

SELECT @YourString=REPLACE(@YourString,SearchFor,ReplaceWith)
FROM @CharsToReplace;

SELECT @YourString;

The result

abc ghf ksjwwl

If you'd define values in ReplaceWith this would work too.

UPDATE A function you can use in any select

Attention: This will be slow!

CREATE FUNCTION dbo.DeleteChars(@CharList NVARCHAR(MAX),@string NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) 
AS
BEGIN
    DECLARE @List XML=(SELECT CAST('<x>'+ REPLACE(@CharList,',','</x><x>')+'</x>' AS XML));
    SELECT @string=REPLACE(@string,ch.value(N'.',N'nvarchar(max)'),'')
    FROM @List.nodes('/x') AS A(ch);
    RETURN @string;
END
GO

--Test this with a mock-up table

DECLARE @SomeTable TABLE(SomeColumn VARCHAR(100));
INSERT INTO @SomeTable VALUES('abc2 ghf5 ksjwwl9'),('123 abc 456 hello');

--This is the char list (comma separated)

DECLARE @CharList VARCHAR(100)='1,2,3,4,5,6,7,8,9,0';

--This is how you call it

SELECT dbo.DeleteChars(@CharList,SomeColumn)
FROM @SomeTable
GO

--Clean up

DROP FUNCTION dbo.DeleteChars;

The result

abc ghf ksjwwl
abc  hello
Community
  • 1
  • 1
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Thanks for the reply, but I don't know which extra characters will be, I want to keep the only letters and remove any extra probable character, for example (0-9, %, $, @, ...) So if I knew the extra characters I could easily replace them with replace() function. – Hossein POURAKBAR Nov 30 '16 at 10:32
  • @HusseinPourakbar Have a look on this answer: http://stackoverflow.com/a/36177685/5089204. This is just meant to filter out all characters with `ASC>=128`, but it will be easy to define the condition to your needs... – Shnugo Nov 30 '16 at 10:35
  • @HusseinPourakbar Btw: If you look at this answer, at the bottom there is a link to another answer, which solves this on a very low *each-character*-level – Shnugo Nov 30 '16 at 10:38