Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« "Combobox" with multiple selections (Visual FoxPro)SQL Server 2008 R2 Editions Pricing Announced »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

There are several string data types in SQL Server. There are varchar, nvarchar, char, and nchar. Most front end languages do not require you to identify the length of string variables, and SQL Server is no exception. When you do not specify the length of your string objects in SQL Server, it applies its own defaults. For most things, the default length is one character. This applies to columns, parameters, and locally declared variables. The notable exception is with the cast and convert functions which default to 30 characters.

How to detect this problem:

SQL 2005 (and up)

  1. SELECT  Name
  2. FROM    sys.sysobjects
  3. WHERE   XType = 'P'
  4.         And Object_Definition(ID) Like '%varchar[^(]%'
  5.         And OBJECTPROPERTY(ID, N'IsMSShipped') = 0
  6. ORDER BY Name

SQL2000

  1. SELECT  Name
  2. FROM    (
  3.         SELECT S.Name, C.TEXT
  4.         FROM   sysobjects S
  5.                INNER Join syscomments C
  6.                  ON  S.id = C.id
  7.                  And S.xtype = 'P'
  8.         WHERE   OBJECTPROPERTY(S.ID, N'IsMSShipped') = 0
  9.  
  10.         UNION All
  11.  
  12.         SELECT OBJECT_NAME(A.id), LeftText + RightText
  13.         FROM   sysobjects s
  14.                INNER Join (
  15.                  SELECT Id, RIGHT(TEXT, 10) AS LeftText, ColId
  16.                  FROM   syscomments
  17.          ) AS A
  18.                    ON  S.id = A.id
  19.                    And OBJECTPROPERTY(S.ID, N'IsMSShipped') = 0
  20.                    And S.xtype = 'P'
  21.                INNER Join (
  22.                  SELECT Id, LEFT(TEXT, 10) AS RightText, ColId
  23.          FROM   syscomments
  24.              ) AS B
  25.                    ON  A.id = B.id
  26.                    and A.ColId = B.ColId - 1
  27.         ) AS A
  28. WHERE   TEXT Like '%varchar[^(]%'
  29. ORDER BY Name

How to correct it: To correct this problem, identify where it exists (using the SQL shown above). Then, for each occurrence, identify the size. If the problem occurred when declaring a column in a table and you WANT the size to be 1 character, then specify (1). Other places, you will need to determine the size that it should be. Sometimes this involves looking up the size in the table definition.

Level of severity: High, because this problem can corrupt your data.

Level of difficulty: Easy.

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
308 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: Naomi [Member] Email
Great post, thanks.
11/06/09 @ 12:33

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)