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

    Search

    XML Feeds

    Google Ads

    « Best Practice: Every table should have a primary keySQL Server 2008 R2 November CTP Available For Download »
    comments

    Column names (and table names) should not have spaces or any other invalid characters in them. This is considered bad practice because it requires you to use square brackets around your names. Square brackets make the code harder to read and understand. The query (presented below) will also highlight columns and tables with numbers in the names. Most of the time, when there is a number in a column name, it represents a de-normalized database. There are exceptions to this rule, so not all occurrences of this problem need to be fixed.

    Based on a comment from Aaron Bertrand, I decided to modify the code below. I recognize that some organizations allow (and may even encourage) the use of the underscore character. In the newly modified code below, you can include a list of acceptable symbols. The code below allows the underscore symbol and the $ symbol. Modify this local variable to include any symbol that is acceptable within your organization.

    How to detect this problem:

    1. Declare @AcceptableSymbols VarChar(100)
    2.  
    3. Set @AcceptableSymbols = '_$'
    4.  
    5. SELECT 'ColumnName' AS Type,
    6.        Table_Name + '.' + Column_Name AS Problem
    7. FROM   Information_Schema.Columns
    8. WHERE  Column_Name like '%[^a-z' + @AcceptableSymbols + ']%'
    9.  
    10. UNION all
    11.  
    12. SELECT  'TableName', Table_Name
    13. FROM    Information_Schema.Tables
    14. WHERE   Table_Name Like '%[^a-z' + @AcceptableSymbols + ']%'

    How to correct it: If this is a number issue, you may need to redesign your database structure to include more tables. For example, if you have a StudentGrade table with (StudentId, Grade1, Grade2, Grade3, Grade4) you should change it to be StudentGrade with (StudentId, Grade, Identifier). Each student would have multiple rows in this table (one for each grade). You would need to add an identifier column to indicate what the grade is for (test on November 10, book report, etc).

    If this is a weird character issue, then you should change the name of the column so it is a simple word or phrase without any spaces, numbers, or symbols. When you do this, make sure you check all occurrences of where this is used from. This could include procedures, function, views, indexes, front end code, etc...

    Level of severity: mild
    Level of difficulty: moderate

    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
    Instapaper

    14 comments

    Comment from: Noel [Visitor] · http://twitter.com/NoelMcKinney
    Noel I keep "weird" characters out of "normal" table names that will go to production. This lets me reserve such characters for use in table names that are for development/test use only and should not go to production databases. My favorite weird character is the dollar sign because it really stands out... For example, I might name a table of development or test results "_$Test$Benchmark$Run1"
    11/10/09 @ 09:29
    Comment from: Aaron Bertrand [Visitor] · http://www.sqlblog.com/blogs/aaron_bertrand/
    Aaron Bertrand George, your check for an "invalid column name" is a little strict. What about underscores? Just because spaces and dashes are bad characters for a column name does not make all symbols taboo.
    11/10/09 @ 10:38
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) Aaron, I dislike any symbols in names. This is my preference, but I can see where different organizations would allow certain symbols in their names. I will modify the code (later today) so that it allows a list of acceptable symbols.

    Thank you for your feedback!
    11/10/09 @ 10:51
    Comment from: Erik [Member] Email
    Erik George, if someone does the totally crazy and says ']' is an acceptable character, the code will break.
    11/12/09 @ 11:52
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky What about - in table names?
    12/04/09 @ 09:46
    Comment from: SQLDenis [Member] Email
    SQLDenis Naomi, table names or actually any object name (proc, database, function, view etc etc) should all follow the same kind of naming conventions
    12/05/09 @ 08:06
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky I forgot right now what caused me to ask this question - some question in one of the forums, but don't remember where.
    12/07/09 @ 11:37
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Here is a sample of the problem
    forum's thread
    05/13/10 @ 10:40
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky What about - in DB Name?

    Check this thread.
    07/06/10 @ 12:19
    Comment from: niikola [Member] Email
    niikola I do not agree with marking numbers as not allowed in column names as there are a lot of situations where column name contains number inside - of course not as leading character.
    08/10/10 @ 03:45
    Comment from: sumit Jain [Visitor]
    sumit Jain Hello,
    If a column name is CurrentDate1 then as per your comment we make it as CurrentDateOne. will it be fine & correct way..
    01/16/12 @ 04:22
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) @sumit,

    In some cases, numbers in a column name may indicate that the table is not normalized. If you have a CurrentDate2, CurrentDate3, etc.... then your table is not normalized and spelling out the number makes no difference.
    01/16/12 @ 05:56
    Comment from: Chattabaugh [Visitor]
    Chattabaugh Columns such as ICD9, CO2E_Value, and even Zip5 are valid column names that do not signify unnormalized data.
    Unnormalized data would requrie two columns with the same name but a sequential number.

    Also in cases were Secquences are requires, we force the name to use "Alternative" words such as "Primary" and "secondary" (i.e. PriamryAddress, SecondaryAddress as opposed to Address1, Address2)

    02/06/12 @ 08:08
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) @Chattabaugh

    I agree that there are times when numbers in columns names are acceptable. Please understand that SQLCop will sometimes raise false positives, as such, you are free to ignore any warning it gives.

    The items that appear in the list of checks are warnings.
    02/06/12 @ 08:41

    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.)