Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remove Spaces in Columns Expand / Collapse
Author
Message
Posted Monday, June 8, 2009 12:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 1:03 AM
Points: 8, 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.
Post #730490
Posted Monday, June 8, 2009 1:06 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 4:33 AM
Points: 1,015, Visits: 1,050

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'

Post #730498
Posted Monday, June 8, 2009 1:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:42 AM
Points: 6,743, Visits: 8,515
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


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- 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"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #730506
Posted Monday, June 8, 2009 1:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:42 AM
Points: 6,743, Visits: 8,515
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


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- 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"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #730508
Posted Monday, June 8, 2009 1:17 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, January 9, 2014 9:24 AM
Points: 186, Visits: 3,073
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

Post #730509
Posted Monday, June 8, 2009 6:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 1:03 AM
Points: 8, Visits: 47
Thanks for your reply. I need to remove spaces in column content only.
Post #730627
Posted Monday, June 8, 2009 10:06 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, January 9, 2014 9:24 AM
Points: 186, Visits: 3,073
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!
Post #730779
Posted Monday, June 8, 2009 10:28 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, November 20, 2014 4:22 PM
Points: 752, Visits: 918
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/
Post #730800
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse