Solved

Rtrim function HElppppppppppppp

Posted on 2003-12-11
7
676 Views
Last Modified: 2012-06-21
I have about 36 fields in a table. All 36 fields need to be trimed. (more specific Rtrim). Is there any way I can Rtrim all 36 fields in one shot instead of typing rtrim(fieldname) for all 36 fields.

Thanks
0
Comment
Question by:proudpaki9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922188
nope
sorry
UPDTE BLah set = Rtrim(Blah) sorry
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922193
You couldve done it by the time it took to post this question and get the answer. How about you type the fields here and il write the query for you
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 9922195
declare @sqlstr varchar(8000)

select @sqlstr=@sqlstr + 'update mytable as X set [' + column_name + '] = rtrim([' + column_name + ']),'
  from information_schema.columns
 where table_schema='dbo'
 and table_nam='mytable'
 and data_type like '%varchar%'

then you can execute it,.,

you could also build the where clause to see if the row actually needs to be updated..
but with 36 columns its probably best to just update them all...

 

0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 4

Expert Comment

by:astankovic
ID: 9922198
I don't think you can.
What you can do is generate a script that will generate the sql statement with necesary fields having rtrim() around them.
If that is in a stored procedure, i guess you could then execute that sql dynamically...
0
 
LVL 6

Expert Comment

by:acampoma
ID: 9922227
Here you go
drop table #sp_Columns
go
Drop Table #Columns
go
Create table #Columns (ColumnId Int Identity(1,1),ColumnNameTxt Varchar(50))
go
Declare @MaxCol int
DEclare @CurrCol Int
DEclare @TableName Varchar(50)
Declare @ColumnName Varchar(50)
declare @SQLText varchar(2000)
set @TableName='T_RISK'



Create Table #sp_Columns (TABLE_QUALIFIER varchar(100),
      TABLE_OWNER varchar(100),
      TABLE_NAME varchar(100),
      COLUMN_NAME varchar(100),
      DATA_TYPE varchar(100),
      [TYPE_NAME] varchar(100),
      [PRECISION] varchar(100),
      LENGTH Int,
      SCALE varchar(100),
      RADIX varchar(100),
      NULLABLE varchar(100),
      REMARKS varchar(3500),
      COLUMN_DEF varchar(100),
      SQL_DATA_TYPE varchar(100),
      SQL_DATETIME_SUB varchar(100),
      CHAR_OCTET_LENGTH varchar(100),
      ORDINAL_POSITION varchar(10),
      IS_NULLABLE varchar(10),
      SS_DATA_TYPE  varchar(100))

Insert Into #sp_Columns exec  sp_Columns @TableName
Insert Into #Columns
select COLUMN_NAME from #sp_Columns


Select @MaxCol=Max(ColumnId),@CurrCol=Min(ColumnId)from #Columns
Set @SQLText='Select '
While @CurrCol<=@MaxCol Begin
select @ColumnName=ColumnNameTxt from #Columns where ColumnId=@CurrCol
set @SQLText=@SQLText+ 'rtrim('+@ColumnName+') as ' +@ColumnName
if @CurrCol<>@MaxCol Begin
      set @SQLText=@SQLText+','
      End      
set @CurrCol=@CurrCol+1
end
Set @SQLText=@SQLText+' from '+@TableName
exec ( @SQLText)
0
 
LVL 6

Expert Comment

by:acampoma
ID: 9922240
There is a line set @TableName='T_RISK'
set the tablename to whatever you want

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 9922402
Yep, best way is to generate the statement, then copy/paste to run it:


DECLARE @sql VARCHAR(8000)

SET @sql = 'UPDATE yourTableName ' + CHAR(13) +
    'SET '

SELECT @sql = @sql + '[' + column_name + '] = RTRIM([' + column_name + ']), ' + CHAR(13) + SPACE(4)
FROM INFORMATION_SCHEMA.columns
WHERE table_name = 'yourTableName'
AND data_type LIKE 'varchar%'

SET @sql = LEFT(RTRIM(@sql), LEN(@sql) - 3)

PRINT @sql
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question