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
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
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
Now you can see that I have converted the hex 09 (tab) to a hex 20 (space).