Remove Spaces in Columns

  • 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.

  • 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'

  • 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

    Learn to play, play to learn !

    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[/url]

    - 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

  • 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

    Learn to play, play to learn !

    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[/url]

    - 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

  • 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

  • Thanks for your reply. I need to remove spaces in column content only.

  • 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!

  • 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/

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply