Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rtrim function HElppppppppppppp

Posted on 2003-12-11
7
Medium Priority
?
681 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 70

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

722 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