-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
DECLARE @colname VARCHAR(32)
DECLARE @coltype VARCHAR(32)
DECLARE @sql VARCHAR(255)

DECLARE c CURSOR FOR
SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't1'

OPEN c

FETCH NEXT FROM c INTO @colname,@coltype
WHILE @@FETCH_STATUS = 0
BEGIN
    IF @coltype = 'varchar'
    BEGIN
        SET @sql = 'UPDATE t1 SET ' + @colname + ' = RTRIM(LTRIM(' + @colname + '))'
        PRINT @sql
        -- EXEC(@sql)
    END
    FETCH NEXT FROM c INTO @colname,@coltype
END

CLOSE c
DEALLOCATE c

GO
