SQL statement help...

Posted on 2014-07-15
Last Modified: 2014-07-27
I am trying to rename the fields and cannot do it .. If I want to rename the following to Department Description, what syntax would I use.  I tried T1.[Segment1 Description] AS Department Description but got an error message.

T1.[Segment1 Description],
Question by:pstre
    LVL 25

    Accepted Solution

    If this is in a SELECT statement, you need to enclose the Alias in either square brackets ( [ ] ) or quotation marks ( " ). The alias cannot contain a space, special characters or reserved words without using them. EX:

    AS [Department Description]
    LVL 21

    Expert Comment

    by:Randy Poole
    Are you trying to return a different name from a select statement or rename the column in the database?
    LVL 65

    Expert Comment

    by:Jim Horn
    Shaun Kline's answer is correct.   T1.[Segment1 Description] AS [Department Description]

    I'll add my .02 in that the only time I would intentionally alias a column with a space is if the object I'm writing is used as the data source for a report, where you want the column headers to be English-user-readable.   Even then I'd consider stating the English-user-readable column header in the report, and not in the SQL code.

    Otherwise, the forcing of square brackets [ ] is one more thing to potentially forget and cause an error.
    LVL 65

    Expert Comment

    by:Jim Horn
    pstre - If you don't mind I'm going to unaccept the answer to allow for a regrade, specifically for a split between myself and Shaun Kline, as EE etiquette doesn't encourage an 'That guy's answer is correct' type comment to receive all points.

    Thanks in advance.

    Author Closing Comment

    Thank you... I was using spaces in the renaming of the column and that was the problem...

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    734 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

    20 Experts available now in Live!

    Get 1:1 Help Now