Solved

See invisible control character in varchar field

Posted on 2006-10-30
9
661 Views
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.
0
Comment
Question by:rlsokoloff
9 Comments
 

Author Comment

by:rlsokoloff
Comment Utility
I meant to post this question in the Microsoft SQL section.  We use Microsoft SQL.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
you could use the sp_hexstring procedure to look at the hex representation of the characters:
http://www.awprofessional.com/articles/article.asp?p=25288&seqNum=7&rl=1

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

0
 

Author Comment

by:rlsokoloff
Comment Utility
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?
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 250 total points
Comment Utility
>> 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:

SELECT *
FROM Table
WHERE CHARINDEX(CHAR(31), Field1) > 0

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



SELECT *
FROM Table
WHERE (
CHARINDEX(CHAR(31), Field1) > 0
OR
CHARINDEX(CHAR(30), Field1) > 0
OR
CHARINDEX(CHAR(29), Field1) > 0
OR
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?

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 8

Expert Comment

by:mnrz
Comment Utility
hi
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.
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
True... first verify what the extra characters are... could they just be spaces?
0
 

Author Comment

by:rlsokoloff
Comment Utility
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.

0
 
LVL 75

Expert Comment

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

Author Comment

by:rlsokoloff
Comment Utility
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.

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now