Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 August SQL Server: Function to remove Non ASCII Characters and Special Characters

SQL Server: Function to remove Non ASCII Characters and Special Characters

JunkData

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I created a function which will remove all non-Ascii characters and special characters from the string of SQL Server.

You can use a below function for your existing data and as well as for new data. The solution of removing special characters or non-Ascii characters are always requirement Database Developers.

Please check the below function and one email example:

Create a function to remove Non-ASCII Chars:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
CREATE FUNCTION [dbo].[fn_RemoveNonASCIIChars]
(
@Inputstring nvarchar(255)
)
RETURNS nvarchar(255)
AS
BEGIN
 
DECLARE @SQL nvarchar(255)
SET @SQL = ''
 
DECLARE @nchar nvarchar(1)
DECLARE @position int
 
SET @position = 1
WHILE @position <= LEN(@Inputstring)
BEGIN
SET @nchar = SUBSTRING(@Inputstring, @position, 1)
--Unicode & ASCII are the same from 1 to 255.
--Only Unicode goes beyond 255
--0 to 31 are non-printable characters
IF (UNICODE(@nchar) between 192 and 198) or (UNICODE(@nchar) between 225 and 230)
SET @nchar = 'a'
IF (UNICODE(@nchar) between 200 and 203) or (UNICODE(@nchar) between 232 and 235)
SET @nchar = 'e'
IF (UNICODE(@nchar) between 204 and 207) or (UNICODE(@nchar) between 236 and 239)
SET @nchar = 'i'
IF (UNICODE(@nchar) between 210 and 214) or (UNICODE(@nchar) between 242 and 246) or (UNICODE(@nchar)=240)
SET @nchar = 'o'
IF (UNICODE(@nchar) between 217 and 220) or (UNICODE(@nchar) between 249 and 252)
SET @nchar = 'u'
IF (UNICODE(@nchar)=199) or (UNICODE(@nchar)=231) -- letter Ç or ç
SET @nchar = 'c'
IF (UNICODE(@nchar)=209) or (UNICODE(@nchar)=241) -- letter Ñ or ñ
SET @nchar = 'n'
IF (UNICODE(@nchar) between 45 and 46) or (UNICODE(@nchar) between 48 and 57) or (UNICODE(@nchar) between 64 and 90) or (UNICODE(@nchar) = 95) or (UNICODE(@nchar) between 97 and 122)
SET @SQL = @SQL + @nchar
SET @position = @position + 1
END
SET @SQL = lower(@SQL) -- emails in lower case
RETURN @SQL
 
END

Test the function:

1
SELECT dbo.fn_RemoveNonASCIIChars('anvesh^&η$%^→&*()*∇+@=gmail.com') AS ValidEmail

Result:

1
2
3
ValidEmail
------------------
anvesh@gmail.com

Aug 18, 2017Anvesh Patel
SQL Server 2012: Introduced the new form of TRY CATCH Exception Handling using THROWSQL Server 2012: Use sp_server_diagnostics to check the health of Server
Comments: 1
  1. Charlie
    May 21, 2019 at 9:08 pm

    I’d like to use this function, but it is also stripping spaces out of my character string. What change can I make to have the function return the space as it is in the string?

Anvesh Patel

Database Engineer

ImageAugust 18, 2017 SQL ServerAnvesh Patel, ASCII, database, database research and development, dbrnd, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....