Is square bracket in table name, column name or datatype is not supported in postgresql ?
I am getting below error while running the query in pgadmin:
CREATE TABLE [Test];
ERROR: syntax error at or near "["
SQL state: 42601
In PostgreSQL you can use double quotes :
CREATE TABLE "Test";
Same for columns, square brackets are used in SQL-Server.
If you mean the table name would be [Test] with brackets included then you would use "[Test]".
Create table "[Test]" ...;
If you meant it as an identifier, you could simply use without brackets or double quotes as Test.
Create table Test ...;
This way, you could refer to it as Test or test or tESt without double quotes in subsequent queries, ie:
select * from test;
If you use "Test" then postgreSQL would treat it as case sensitive and you would always use "Test".
Create table "Test" ...;
If you were trying to emphasize the identifier name by using square brackets, then sagi's answer is correct. On the other hand, if you really want to use square brackets in your table name, postgresql supports this as "[Test]". In this case your table name will include square brackets. You can get additional info from postgresql documentation
Related
I know this is very bad naming practice, but I am not the owner of the table...
I need to run :
SELECT COL_"NAME"
FROM TABLE
where COL_"NAME" is the name of the column, containing double quotes.
I tried :
SELECT COL_""NAME""
FROM TABLE
SELECT COL_\"NAME\"
FROM TABLE
But nothing works
Identifier Requirements:
To use the double quote character inside a quoted identifier, use two quotes.
To access column: COL_"NAME"
SELECT "COL_""NAME"""
FROM TABLE;
We have Oracle table having default keyword(i.e in as field name) field name.Now i am querying table but unable to extract specific field data.
select a.filename,a.in from table a
Following error appears "invalid field name.
Try using double quotes.
select a."IN" from table a
You can use default (oracle reserved) keywords as the name of the columns but yes it is not advisable to use it.
Anyway, If you want to use oracle reserved keywords then you must have to enclose them in the double-quotes.
Note that oracle is case insensitive in terms of its object names until and unless it is wrapped in the double-quotes. it means if you enclose any object name in double-quotes then you must have to use them anywhere in the entire DB as case sensitive manner.
So if your table definition is:
CREATE TABLE YOUR_TABLE ("IN" NUMBER);
Then you need to use "IN" wherever you want to refer the column but if your table definition is:
CREATE TABLE YOUR_TABLE ("in" NUMBER);
Then you need to use "in" wherever you want to refer the column. -- case sensitive names.
I hope it will clear all your doubts.
Cheers!!
I have a database with a Table named PaymentReciept I was playing around and found some unexpected results from a simple select all query.
// Return 3139 rows
select * from PaymentReciept
// Return 3139 rows
select * from dbo.PaymentReciept
// Return 0 rows
select * from [dbo.PaymentReciept]
I don't understand why the query with square brackets return no results. I know that square brackets are used as delimiters for identifiers that use some special character or a keyword.
Can someone kindly explain to me what's going on or direct me to some article or a book where I can learn more about this behavior.
[dbo.PaymentReciept]
Means that SQL is querying a table whose name is dbo.PaymentReceipt
whereas
dbo.PaymentReciept and PaymentReciept means that SQL is querying a table called PaymentReceipt in the schema dbo.
To use square brackets correctly, use:
[dbo].[PaymentReciept]
Square brackets should be placed around schema name (dbo) and the table name (PaymentReciept), as these two are separate objects:
select * from [dbo].[PaymentReciept]
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.
when I was renaming the column in SQL Server, I accidentally inserted the square brackets around the column. The actual statement that I used was:
SP_RENAME 'customer.[EMPLOYEENAMES]', '[EMPLOYEENAME]', 'COLUMN'
But when I try to retrieve the data it just says and I even tried with out square brackets, it gives the same error
Invalid column name '[EMPLOYEENAME]'.
How should I remove the square brackets.
This will restore order in your database:
EXEC SP_RENAME 'customer."[EmployeeName]"', 'EmployeeName','COLUMN'
You cannot use double brackets because it returns syntax error. Quotes circumvent this limitation.
As you've now got a column with square brackets in the name itself, you can access that column by:
SELECT [[EmployeeName]]]
FROM Customer
Yes, all those extra square brackets are a bit unwieldy :)
So, I'd rename your column again to remove the brackets:
EXEC SP_RENAME 'customer.[[EmployeeName]]]', 'EmployeeName','COLUMN'
So you can then reference "normally":
SELECT EmployeeName
FROM Customer
None of the given alternatives worked for me, so I had to create a new column with the right name, do an update setting newName = [[badName]]], and then drop the wrongly named column.