Bobski Bobski - 1 year ago 69
SQL Question

Sql server - select correct "Text" values as new value

i have a query where I'm selecting information from two different tables - I'm doing an inner join and pulling up text values instead of ID's. What's happening tho is the headers for all these columns are not in text format that I want it to be. In my application I have to change all the headers. Here's my query...

SELECT IDClient,t2.Name Dept,t3.Name Position,Salary
FROM TblClientData t1
INNER JOIN tblDataRef t2 ON t1.Dept = t2.ID
INNER JOIN tblDataRef t3 ON t1.Position = t3.ID
WHERE IDClient = 177

Basically what I'm looking to do is instead of

SELECT IDClient, t2.Name Dept

Where t2.Name Dept will have a heading Dept

I want to name it something different, something like

SELECT IDClient, t2.Name Dept as [Department]

When i do this I get an error....

Answer Source

You have both Dept and `Department. You only need one:

SELECT IDClient, t2.Name as Department

I always use as with column aliases. It makes it easier to spot error like this.

Also, there is no need to escape Department. I find the square braces to be a bit messy when they are not needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download