dcsimg
Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
SQL Scripts & Samples
Tips
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
DatabaseDaily  

By submitting your information, you agree that databasejournal.com may send you databasejournal offers via email, phone and text message, as well as email offers about other products and services that databasejournal believes may be of interest to you. databasejournal will process your information in accordance with the Quinstreet Privacy Policy.

News Via RSS Feed


Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

Database User and Programming Tips

Posted September 4, 2018

WEBINAR:
On-Demand

The California Consumer Privacy Act: What You Need to Know


Displaying and Removing Unprintable Hexadecimal Characters from SQL Server Data

By Greg Larsen

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



Database User and Programming Tips Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.