tags:

views:

58

answers:

2

I'm trying to rename the columns. The syntax should be the column name between double quotes incase of two words, like this:

SELECT p_Name "Product Name" from items

So I'm trying to do it in C# code like this:

string sqlqry1 = "SELECT p_Name \"Prodcut Name\" from items";

But I get an error:

Syntax error (missing operator) in query expression 'p_Name "Prodcut Name"'.

It seems am having somthing wrong with the quotes, but I can't figure out.

+1  A: 

You are missing an as:

string sqlqry1 = "SELECT p_Name as \"Prodcut Name\" from items";
Darin Dimitrov
Rubbish. it's optional. See http://msdn.microsoft.com/en-us/library/ms176104.aspx it says `...[ [ AS ] column_alias ]...`
gbn
@gbn: Why are you citing microsoft docs when the OP did not specify what database he is using?
Larry Lustig
@Larry Lustig: care to bet? Does every DBMS require AS too?
gbn
Not "every" DBMS, but some do. Why introduce possible complications while debugging?
Larry Lustig
@gbn: you're right. I don't know any database server that requires "as". DB2, MySql, Sqlite, SqlServer, Oracle, ... They all don't care
Philippe Leybaert
+3  A: 

You don't specify what database you're using. Different DBMSes use different quoting systems for identifiers (like column names). Try:

 SELECT p_Name AS [Product Name] FROM items

or

 SELECT p_Name AS `Product Name` FROM items

which are two common systems. Also, use the AS specifier even though some DBMSes allow you to leave it out.

(PS: In the second example, the quote character is the backtick, generally on the same key as the tilde (~) on US keyboards).

Larry Lustig
Thanks, that worked.
DanSogaard
@DanSogaard: What worked?
gbn
Always a pleasure to be of help.
Larry Lustig
@gbn: I tried the first statement and its working without any errors: SELECT p_Name AS [Product Name] FROM items
DanSogaard