Syntax error when selecting column 'Comment' in Sybase IQ - sql

I have a table A with some columns in Sybase IQ. One of the columns is named "Comment".
Whenever I select that column:
select Comment from A
I got the error:
[Error Code: 102, SQL State: 42W04] SQL Anywhere Error -131: Syntax error near 'Comment'
I am able to select other columns without issues. Could you advise the reason and solution? Thank you

Try
select "Comment" from A
COMMENT is a reserved word in Sybase IQ.
Here is the link explaining your problem.
Some keywords in SQL are also reserved words. To use a reserved word
in a SQL statement as an identifier, you must enclose the word in
double quotes. Many, but not all, of the keywords that appear in SQL
statements are reserved words. For example, you must use the following
syntax to retrieve the contents of a table named SELECT.
SELECT * FROM "SELECT"

Related

The SELECT statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect

I'm trying to create a query using two tables I got the following message:
The SELECT statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect
I am SQL ignorant.
FROM [UIC ROSTER]
INNER JOIN [Azusa RSP] ON [UIC ROSTER].UIC = [Azusa RSP].UIC

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

Left join ON not null columns can't select not null column

Each table has a column RECNUM. They are (decimal(28,0), not null). That is where I am doing my join. I want to select the column DESC in CAUNIT. It is (varchar(28,0), not null). When I run my query I get:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'DESC'. Below is my query:
SELECT CDCLSVC.UNIT_ID,CAUNIT.DESC
FROM CDCLSVC
LEFT JOIN CAUNIT
ON CDCLSVC.RECNUM = CAUNIT.RECNUM
The problem is with DESC column. In SQL Server it is a reserved keyword:
Microsoft SQL Server uses reserved keywords for defining,
manipulating, and accessing databases. Reserved keywords are part of
the grammar of the Transact-SQL language that is used by SQL Server to
parse and understand Transact-SQL statements and batches. Although it
is syntactically possible to use SQL Server reserved keywords as
identifiers and object names in Transact-SQL scripts, you can do this
only by using delimited identifiers.
Possible solution:
Rename column e.g. description
Quote it with []
You could also use aliases to avoid typing full table names:
SELECT cd.UNIT_ID,ca.[DESC]
FROM CDCLSVC cd
LEFT JOIN CAUNIT ca
ON cd.RECNUM = ca.RECNUM

How do I deal with SQL tablenames with hyphen (-) when writing raw queries? i.e project-users

I have a table called project-users and want to write a SQL query like SELECT * FROM project-users I get this error ERROR: syntax error at or near "-".
I cannot change the table name at this point.
According to http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html, you should use double quotes.
In your case, for PostgreSQL the query should be:
SELECT * FROM "project-users";
It is good practice to avoid the use of characters that need escaping or that contain spaces in identifiers.

Select table with column named "index"

I have a table with a column name called "index"
select id, index
from item;
gives an error:
Msg 1018, Level 15, State 1, Line 1 Incorrect syntax near 'index'. If
this is intended as a part of a table hint, A WITH keyword and
parenthesis are now required. See SQL Server Books Online for proper
syntax.
How can I do a select on a column named index? I'm using sqlserver 2008-R2
Use square brackets to quote reserved words:
select id, [index]
from item
See also the documentation on Delimited Identifiers.
Put reserved words in brackets:
select id, [index]
from item
Try this
SELECT id, [index] FROM item
Reserved words used as names in SQL-Server must be enclosed in brackets.