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
 General SQL Server Forums
 New to SQL Server Programming
 replace space with underscore

Author  Topic 

angelntn
Starting Member

2 Posts

Posted - 2012-01-31 : 19:56:15
hi. i'm a noob of sql.
i wanna know if is possible to create a query in sql that can replace the name of columns in a table where there are spaces with the same name with underscore.

ex. i have this table: teachers
and this columns: id teacher
name
surname
monthly salary

i wanna this:
column: id_teacher
name
surname
monthly_salary

i know that there's a function call replace().
someone could help me?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-31 : 19:58:17
use below


EXEC sp_rename 'teachers.[monthly salary]', 'monthly_salary', 'COLUMN';


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

angelntn
Starting Member

2 Posts

Posted - 2012-01-31 : 20:52:55
ok. perfect.
but i need something like a procedure.this is my problem.

i wanna migrate my db from access to sql server.
i use ssma for this but a lot of columns in my db have a space in the name. (ex. [monthly salary])
ssma reports to me a lot of warnings for this identifiers.

could i make a procedure for replace the name of all this columns????
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-01 : 09:29:49
why not write a logic to serach within INFORMATION_SCHEMA.COLUMNS view for columns with space in between and then call sp_rename to make them _ instead?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -