Sql tricky Interview Question : How you will select a column that contain square brackets[]?
Tricky Interview questions:
Question: If you have a column with square brackets like below one, how you will select it in SQL query?
Answers: for this you need to use two square brackets before and three square brackets after the column name as below.
Note : I face problem in selection when I rename my column as [DefautlProjectActivityID] by using following proc.
EXEC SP_RENAME '[TableName].[DefaultProjectActivityID]' , '[DefaultProjectActivityID]', 'COLUMN'
correct one is :
EXEC SP_RENAME '[TableName].[DefaultProjectActivityID]' , 'DefaultProjectActivityID', 'COLUMN'
Note that you should not put the NewColumnName in brackets, as SQL Server will then use [NewColumnName] as the name of the new column.
Question: If you have a column with square brackets like below one, how you will select it in SQL query?
Answers: for this you need to use two square brackets before and three square brackets after the column name as below.
Note : I face problem in selection when I rename my column as [DefautlProjectActivityID] by using following proc.
EXEC SP_RENAME '[TableName].[DefaultProjectActivityID]' , '[DefaultProjectActivityID]', 'COLUMN'
correct one is :
EXEC SP_RENAME '[TableName].[DefaultProjectActivityID]' , 'DefaultProjectActivityID', 'COLUMN'
Note that you should not put the NewColumnName in brackets, as SQL Server will then use [NewColumnName] as the name of the new column.
Best blog
ReplyDelete