Do you know if there\'s a quick way in sql server (via transact-sql) that I could trim all the database string fields.
OK, that was quick and dirty but i have been sufficiently motivated by a current project to do this 'properly' - and with no cursors either, but a little sql concatenation trick. Does use dynamic sql though:
--exec spGenerateTrimStatements 'StaticImportMaturities'
ALTER PROCEDURE spGenerateTrimStatements
(
@TableName NVARCHAR(100)
)
AS
DECLARE @Cr char(2),
@OutputString nvarchar(max)
SELECT @Cr = CHAR(13) + CHAR(10)
SET NOCOUNT ON
-- Create table to store commands
CREATE TABLE #tOutput(OutputText nvarchar(500), RowID int identity(1,1))
-- Build up commands
INSERT #tOutput(OutputText)
SELECT 'UPDATE ' + @TableName + ' SET '
INSERT #tOutput(OutputText)
SELECT '[' + Column_Name + '] = ' + 'LTRIM(RTRIM([' + Column_Name + '])), '
FROM INFORMATION_SCHEMA.Columns
WHERE Table_Name = @TableName
AND Data_Type LIKE '%CHAR%'
-- Trim last comma
UPDATE #tOutput
SET OutputText = LEFT(OutputText, LEN(OutputText)-1)
WHERE RowID = (SELECT Max(RowID) FROM #tOutput)
-- use subselect to concatenate the command string
SELECT @OutputString = ISNULL(@OutputString, '') + ISNULL(OutputText, '')
FROM (SELECT OutputText
FROM #tOutput) TextOutput
-- run the command
EXEC sp_ExecuteSQL @OutputString