Solved

Rtrim function HElppppppppppppp

Posted on 2003-12-11
7
675 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL - format decimal in a string 5 38
SQL Backup Question 2 29
Access SQL Server instance by NONE-admin Windows user 12 26
HTML <font style="color:red"> 9 27
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

790 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