MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old January 19th, 2004, 10:02 PM
iso iso is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Posts: 506 iso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 10 h 14 m 25 sec
Reputation Power: 5
cleaning values

is there a quick way to do this within sql.

i am thinking of something along the lines of what would be an equivalent to something in PHP (obviously not a rdbms) like
foreach ($ArrayName as $varName=>$value)
{
$value = rtrim(ltrim($value));
}

instead of having to do each variable manually.

Thanks for the help

Reply With Quote
  #2  
Old January 19th, 2004, 11:34 PM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 31 m 56 sec
Reputation Power: 8
One reason this will not work is because TSQL variables are strong typed. You would error out if you attempted this with a date or number variable.
One option is to create a function that contains all the variables called resetVariables that does nothing but reset the variables.
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Reply With Quote
  #3  
Old January 19th, 2004, 11:42 PM
iso iso is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Posts: 506 iso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 10 h 14 m 25 sec
Reputation Power: 5
thanks for the help. I was hoping there was a quick way.

Reply With Quote
  #4  
Old January 20th, 2004, 01:13 AM
abombss abombss is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 49 abombss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
One reason this will not work is because TSQL variables are strong typed. You would error out if you attempted this with a date or number variable.


Another reason is MS-SQL has no concept of arrays. You would have to use cursors and it would be slow and ugly, and like victor pointed out you would run into issues with strongly typed columns.

If you are using varchar fields they will be automatically trimmed. If you are using char fields they will contain leading and trailing spaces.

If you are storing variable width data use a varchar and trim it on the way in through a sproc. Then you do not need to trim them on the way out. Otherwise just trim the columns on the way out in a select..
Code:
select ltrim(rtrim( nme ) ) from dbo.fooBar where id = 1


You can also update a table and trim the values, however this will do no good if they are stored type char.

Code:
create table fooBar(
    id int identity(1,1) primary key,
    stat varchar(10) check (stat IN('Retired', 'Playing', 'Injured') ),
    nme varchar(10),
    foo int
)
go

create procedure insert_foobar( @stat varchar(10), @nme varchar(10), @foo int )
as
begin
    insert into dbo.fooBar ( stat, nme, foo ) values( ltrim(rtrim(@stat)), ltrim(rtrim(@nme)), @foo )

end
go

Last edited by abombss : January 20th, 2004 at 01:20 AM.

Reply With Quote
  #5  
Old January 20th, 2004, 08:08 AM
abombss abombss is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 49 abombss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I might have misunerstood your question. You can do a mass update to all the rows in a table with an update query.
Code:
update dbo.fooBar set stat = ltrim(rtrim(stat))

This will trim all the rows in the column named stat in the fooBar table.Careful... If you do not use a where clause you could update some data you don't want to.The following example uses a where clause to limit the update to rows with a col1 value of 3.

Adam
Code:
update dbo.fooBar set col2 = ltrim(rtrim(col2)), col3 = upper(col3)
where col1 = 3

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > cleaning values


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway