SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remove Spaces in Columns


Remove Spaces in Columns

Author
Message
saranram_forum
saranram_forum
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 47
Hi,
I have problem after exporting excel data to sql server. I got whitespaces in columns and I need to remove whitespaces in all columns in a single query. I can remove the whitespaces single column at a time.But I need to remove whitespaces in all columns at a time.Is there any possiblity to remove whitespaces in all columns at a time? Please provide solution.
srikant maurya
srikant maurya
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1139 Visits: 1061
try below sql statment with admin permision
update sc
set sc.name=replace(sc.name,' ', '')
from sys.syscolumns sc,sys.sysobjects so
where so.id=sc.id and so.type ='u' and charindex(' ',sc.name)>0
--and so.name='table_name'
ALZDBA
ALZDBA
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12085 Visits: 8924
One ambiguity overhere:
- are you talking of white spaces in the column names ?
- are you talking of white spaces in the columns content ?


To alter the column names, you could just generate the alter column statements and then execute that in a sql batch.

This will get you the concerning names:
print @@servername + ' / ' + db_name()
go

SELECT [TABLE_CATALOG], [TABLE_SCHEMA], [TABLE_NAME], [COLUMN_NAME], [ORDINAL_POSITION], [COLUMN_DEFAULT], [IS_NULLABLE], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [CHARACTER_OCTET_LENGTH], [NUMERIC_PRECISION], [NUMERIC_PRECISION_RADIX], [NUMERIC_SCALE], [DATETIME_PRECISION], [CHARACTER_SET_CATALOG], [CHARACTER_SET_SCHEMA], [CHARACTER_SET_NAME], [COLLATION_CATALOG], [COLLATION_SCHEMA], [COLLATION_NAME], [DOMAIN_CATALOG], [DOMAIN_SCHEMA], [DOMAIN_NAME]
FROM [INFORMATION_SCHEMA].[COLUMNS]
WHERE [column_name] like '% %'
ORDER BY [TABLE_CATALOG], [TABLE_SCHEMA], [TABLE_NAME], [COLUMN_NAME]



Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
ALZDBA
ALZDBA
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12085 Visits: 8924
srikant maurya (6/8/2009)

try below sql statment with admin permision
update sc
set sc.name=replace(sc.name,' ', '')
from sys.syscolumns sc,sys.sysobjects so
where so.id=sc.id and so.type ='u' and charindex(' ',sc.name)>0
--and so.name='table_name'



If I'm correct this will no longer work with SQL2005, since it doesn't allow direct catalog updates anymore!

Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Allister Reid
Allister Reid
SSC Veteran
SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)

Group: General Forum Members
Points: 288 Visits: 3073
here's a possible solution, depends on what you mean by 'whitespace', I'm assuming space i.e. character 20.


create table #temptest ([some col] int, [some othercol] int)
select * from #temptest

declare @tbl nvarchar(255)

select @tbl = '#temptest'

declare @sql nvarchar(max)

select @sql = coalesce(@sql, '' ) + '
exec sp_rename ''[' + @tbl + '].[' + syscolumns.name + ']'', ''' + replace(syscolumns.name,' ','') + ''''
from sysobjects, syscolumns
where
sysobjects.id = syscolumns.id
and sysobjects.id = object_id('[' + @tbl + ']')
and syscolumns.name like '% %'

print @sql

exec (@sql)
select * from #temptest
drop table #temptest


saranram_forum
saranram_forum
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 47
Thanks for your reply. I need to remove spaces in column content only.
Allister Reid
Allister Reid
SSC Veteran
SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)

Group: General Forum Members
Points: 288 Visits: 3073
Heh heh, solutions above are for removing whitespace in column names.

If you post some sample data, and table structure, you are much more likely to get a useful solution.

See http://www.sqlservercentral.com/articles/Best+Practices/61537/ for how to post data in the way most likely to get a useful response!
timothyawiseman
timothyawiseman
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1212 Visits: 920
First, by white space I am going to assume you mean spaces. If you need to include tabs, carriage returns, and perhaps even other nonprinting characters then it gets more complicated. You could of course just keep adding replace functions until you have covered every type of whitespace you need, but if you really mean any whitespace at all, you may want to consider writing something in a CLR/DLR language like C# or IronPython that let you use regex and other more powerful string manipulation for that.

But if you just mean spaces, and you already have the data inside of sql, then I would use dynamic sql and do something like this:
(Note: This is untested and written off the cuff)

declare @sql varchar(8000

select @sql = 'update YourTableName set'

--char(10) gives a line break and simply makes it easier to read when proof reading
select @sql = @sql + column_name + ' = replace(' + column_name + ', '' '', ''''), ' + char(10)
from infromation_schema.columns
where table_name = YourTableName

--Remove the final comma
select @sql = substring(@sql, 1, len(@sql) -1)

--Preview the code this generates first
print @sql

-- exec @sql --remove the comment marks and let it run the code only after previewing it once



That should do it if you want to remove all spaces from all columns and all columns are of chracter type. If you want to exclude some columns or have noncharacter columns in there, then add a where clause to the select statement that populates the columns in the @sql statement.

---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search