See invisible control character in varchar field

Posted on 2006-10-30
Last Modified: 2007-12-19
Every once in awhile someone seems to enter an invisible control character in a varchar field through my application.  When the dataset containing this record is selected and loaded into another application, problems occur.  I never know what the actual character is, but the len() function reveals more characters than I can acutally see.  It would be nice if I could see these invisilbe control characters by running a select statement in SQL Query Analyzer.

QUESTION:  When running a select statement in Query Analyzer against a varchar or char field, is there a way to also see the invisible characters, e.g. control characters, carriage returns, new line, etc.
Question by:rlsokoloff
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 17836850
I meant to post this question in the Microsoft SQL section.  We use Microsoft SQL.
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17836854
you could use the sp_hexstring procedure to look at the hex representation of the characters:

the normal space id ascii(32), ie hex(2D)


Author Comment

ID: 17837536
I'm not sure how this sp_hexstring procedure would be useful to my question.  All it seems to do is take an integer represented as a varchar(10) and returns the hexidecimal representation of the integer.   I don't believe this answers my question.  Do you have more info?
Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

LVL 30

Accepted Solution

nmcdermaid earned 250 total points
ID: 17838327
>> When the dataset containing this record is selected and loaded into another application, problems occur

The best place to do it is probably whatever you are using to load the data. This is called a 'data cleansing' step.

In answer to your question, here is a way to find non-printing chracters:

FROM Table

This particular select should find all records where Field1 contains char(31) - a non printing character.

FROM Table
CHARINDEX(CHAR(31), Field1) > 0
CHARINDEX(CHAR(30), Field1) > 0
CHARINDEX(CHAR(29), Field1) > 0
CHARINDEX(CHAR(28), Field1) > 0

This one will find records that contain CHAR 31,30,29 or 28

As you can see you need to extend this for all the other non printable characters - all the way down to zero, and all the way from 128 to 256

I'm sure there are a number of smarter ways to do it but this way is self explanatory.

So how on earth do people get non-printing characters in there?


Expert Comment

ID: 17840150
maybe there is no control character and users have entered unicode characters and this cause its length apear more that usual. however if you sure there is control characters have been entered you can replace with any other char you know.
LVL 30

Expert Comment

ID: 17840674
True... first verify what the extra characters are... could they just be spaces?

Author Comment

ID: 17843545
Thanks for your comments!  I will explore the suggestions in more detail.

I have no idea how they got non-printing characters in there.  They must be hitting control something when typing info into  a textbox that then gets copied to the corresponding varchar field in the SQL Server database.  It rarely happens.  I assume it's a control character, because it termintates the rest of the record in the recordset that my applciation copies into a 3d-party spreadsheet (fpSpread from Farpoint).  It is not a space for sure.  But when I apply a length function to the problem field in the record, it clearly shows the length is longer than the number of visible characters.

I'd like to verify what the extra charactes are--that was my question.  They are not spaces.

LVL 75

Expert Comment

by:Anthony Perkins
ID: 17845590
>>I'd like to verify what the extra charactes are<<
That was what angelIII was trying to help you with.

Author Comment

ID: 17891174
Thank you for the responses.  I don't think any of the answers really fit, although nmcdermaid came closest, bringing to my attention the charindex function().  

I believe the problem occurs with the last character in the problem record of the problem field.  The next time I encounter the problem, I will do something like the following to ID the maverick character.....

     select ascii( right(fieldname, 1))
     from tablename
     order by ascii(right(fieldname, 1))

where the actual names are substituted for fieldname and table.


Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question