Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Column names containing spaces

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-01-18 : 15:54:10
I have a large number of tables, created by someone else, which have been created with column names containing spaces. I think these can be referenced using back ticks (unless I'm wrong, in which case please correct me), but I don't want to do this, I would rather use underscores.

How can I easily rename all the columns in a table, which contain spaces, and replace these with underscores please?

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-18 : 16:10:01
You can use sp_rename to rename a column of a table. Here is an example from SQL Server help:

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'


But make sure, that you may break all the stored proc and functions which refer that column.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-21 : 10:16:52
Output of this may help you

select 'EXEC sp_rename '''+table_name+'.['+column_name+']'',''['+column_name+']'','''+replace(column_name,' ','_')+''''
from information_schema.columns
where column_name like '% %'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2008-01-21 : 10:28:23
Many thanks for that - much appreciated (and to everyone else who replied)
Go to Top of Page
   

- Advertisement -