Skip to content

Instantly share code, notes, and snippets.

@othtim
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save othtim/9904634 to your computer and use it in GitHub Desktop.

Select an option

Save othtim/9904634 to your computer and use it in GitHub Desktop.
Check the database for fields with trailing blanks
drop table #temptable
select sObj.name as cTable, sCol.name as cColumn
into #temptable
from sys.tables sTable
inner join sys.columns sCol
on sCol.object_id = sTable.object_id
inner join sys.objects sObj
on sCol.object_id = sObj.object_id
inner join sys.types sType
on sCol.system_type_id=sType.system_type_id and sType.is_user_defined=0
where sType.name = 'varchar'
and (SCHEMA_NAME(sTable.schema_id)=N'dbo')
--and sObj.name like '[C]%'
order by sObj.name, sCol.column_id
declare @TableName varchar(50);
declare @ColumnName varchar(50);
declare @commandstring varchar(500);
declare cOuter cursor for
select distinct cTable from #temptable
open cOuter
fetch cOuter into @TableName
while @@fetch_status=0
begin
declare cInner cursor for
select cColumn from #temptable where cTable like @TableName
open cInner
fetch cInner into @ColumnName
while @@fetch_status=0
begin
SET NOCOUNT ON
--set @commandstring = 'UPDATE ' + @TableName + ' SET ' + @ColumnName + ' = LTRIM(RTRIM(' + @ColumnName + '))'
set @commandstring = 'SELECT * FROM ' + @TableName + ' WHERE ' + @ColumnName + ' LIKE ''%[ ][ ]'' '
select @commandstring
exec(@commandstring)
fetch next from cInner into @ColumnName
end
close cInner
deallocate cInner
fetch next from cOuter into @TableName
end
close cOuter
deallocate cOuter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment