SHARE
Facebook X Pinterest WhatsApp

Displaying and Removing Unprintable Hexadecimal Characters from SQL Server Data

Written By
thumbnail
Gregory Larsen
Gregory Larsen
Sep 4, 2018

There are times when you might need to look at your SQL Server data in hexadecimal format.  Occasionally some string data in my database will contain unprintable characters erroneously.  These unprintable characters can cause downstream problems, if not detected and removed.  For instance, there are times that a tab character will get into my SQL Server table data.  When this occurs, and I extract the data to a tab delimited file, the file will contain an extra tab character, which causes downstream processes to have problems when it detects an extra tab character. 

When a tab gets in your data it might be hard for you to see it because it is unprintable.  Here is an example of where I’ve placed a tab character right in the middle of my variable @String:

-- insert a TAB character into the middle of my string variable
DECLARE @String varchar(30) = 'This is' + cast(0x09 as varchar(1)) + 'my string';
SELECT @String as MyString 
 

When I run this code, I get the following output:

Insert a TAB character into the middle of my string variable
Insert a TAB character into the middle of my string variable

As you can see the hex “09” character just shows up like a space in my data.  I can display that tab character by displaying my data in hexadecimal.  To accomplish that I run the following code:

DECLARE @String varchar(30) = 'This is' + cast(0x09 as varchar(1)) + 'my string';
SELECT @String as MyString, CAST(@String as varbinary(max)) MyString_In_Hexidecimal;

When I run this snippet of code I get the following output:

Display the TAB character
Display the TAB character

Once I display the data in hexadecimal I can see the tab character (hex 09) in the string (see yellow text) above.

If I want to replace that hex 09 with a space I can run the following code:

DECLARE @String varchar(30) = 'This is' + cast(0x09 as varchar(1)) + 'my string';
SET @String = REPLACE(@String,0x09,' ');
SELECT @String as MyString, CAST(@String as varbinary(max)) MyString_In_Hexidecimal;

Here I can use the REPLACE function to replace a hex 09 character in the @String variable to a space (‘ ‘). When I run this code, I get the following output:

Replace a hex 09 character
Replace a hex 09 character

Now you can see that I have converted the hex 09 (tab) to a hex 20 (space).  

See all articles by Greg Larsen

Recommended for you...

Best Online Courses to Learn SQL
Ronnie Payne
Sep 23, 2022
Best Courses for Database Administrators
Ronnie Payne
Jul 22, 2022
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
How Many Databases Can You Name?
Brad Jones
May 11, 2020
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.