Removing [] around column in SQL Server 2005 - sql-server-2005

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.

Related

single quote in column name: SQL expression working fine in SQL Developer but failing in PowerBI

I do have a piece of SQL code that gave me some problems. The pivot operation results in a table with a column name called 'INITIAL' which has the single quotes in the name. But the values in that column are integers, or NULLs. I managed to access the numbers in SQL developer by enclosing the column name with double quotes: "'INITIAL'". When copying the code to PowerBI, it is not accepted. I returns a
Expression.SyntaxError: Token Comma expected
at the location of the first use of the single quote.
Any idea how to fix this for powerBI?
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM("'INITIAL'")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST
that nearly did the trick. I misread your post and used this:
SUM(""'02_INITIAL'"")
And that works in PowerBI!
So in SQL Developer I need to use SUM("'INITIAL'")
Best is fo course to avoid quotes in column names, but since my Pivot is based on string values, I get them for free.
Many thanks for your suggestion that put me on the right track!
To escape a character you need to use double quotation ("") mark. Please try this:
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM(""'INITIAL'"")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST

Using a bit column in a where clause

I am writing a very simple query where I need to retrieve the records based on a column that has two values: 0 and 1. I didn't realize this column has a bit type, therefore when I write the query SQL Server is giving me the message
"Incorrect syntax near the keyword 'Primary'."
My query is simple:
select * from [table name]
where Primary = '1'
I've tried searching the site but couldn't find a good answer. BTW, I only have access to retrieve the data from the table. I can't declare variables or create a stored procedure or any of that stuff. Surely this can't be that complicated. Please assist!
PRIMARY is reserved word and it needs to be quoted:
select * from [table name] where [Primary] = 1;
The problem has nothing to do with the datatype being a bit, the error, in fact, is telling you exactly what the problem is:
Incorrect syntax near the keyword 'Primary'.
Emphasis added.
PRIMARY is a reserved keyword. Ideally don't use keywords for object names, but if you "must" then you must delimit identify the object. In fact, really you should avoid any names for objects that require delimit identifing:
SELECT {Your Columns} --Define the columns, don't use *
FROM dbo.[Table Name] --I hope you don't have white space in your object names too
WHERE [Primary] = 1; --Don't wrap bit/numerical values in quotes.
That's because primary is a reserved word in SQL Server. You would need to quote it:
select * from [table name] where [Primary] = 1
I would warmly recommend changing the column name so it does not conflict with a langauge keyword. There are less than 200 reserved works in SQL Server, which leaves a lot of room for alternatives.
So:
sp_rename '[table name].[primary]', 'prim', 'COLUMN';

CQL Query on a column with a whitespace

I wish to do a SELECT query but unfortunately, the database I am working on has a column with whitespace.
Example of query (partial) :
"WHERE My Column = value"
CQL says that
the column name 'My' doesn't exist in the database.
How can I make CQL understand that it has to look for the column "My Column"?
Thank you in advance.
you can execute query with following change
"WHERE [My Column] = value"
SQL will take care of blank space between column name. As for blank space column or table name can be assigned to database but you can use it with square bracket ([]).
I found what worked for me :
'WHERE "My Column" = value'

Square bracket in table/column name is not supported?

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

Rename a column with space in it

Someone mistakenly created a table in which all the column names has a leading space in it. For example: 'accountid' is now ' accountid'.
I am going to write a SQL statement to rename these columns. The one I wrote is:
ALTER TABLE mytable RENAME COLUMN ' accountid' TO 'accountid';
However, I got the following error:
Error : ERROR: syntax error at or near "' accountid'"
Can someone instruct me how to rename these? How to change my statement to make it runnable? I use PostgreSQL.
Many thanks.
In PostgreSQL, you use double-quotes for identifiers (if necessary): "
ALTER TABLE mytable RENAME COLUMN " accountid" TO "accountid";
See here and browse to 4.1.1
You can even put other characters:
select c.comment "actor.comment" from post p join comment c on p.id = c.post_id;