SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How avoiding Varchar field triming space character
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Mathias
Posting Yak Master

France
114 Posts

Posted - 03/02/2004 :  03:30:29  Show Profile  Reply with Quote
I need to create a table to enter record various strings that should be converted to a new value. One of the change that I need to record is: change space to null.
create TABLE [TEXT_CONVERSION] (
[REC_NUM] [int] IDENTITY (1, 1) NOT NULL ,
[SOURCE_STR] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[CONVERTED_STR] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
) ON [PRIMARY]

I don't manage to record the space in the varchar field.
Thanks, Paul

rihardh
Constraint Violating Yak Guru

Slovenia
307 Posts

Posted - 03/02/2004 :  03:54:46  Show Profile  Reply with Quote
Change space to null? I don't get it.

More info please...
Go to Top of Page

Mathias
Posting Yak Master

France
114 Posts

Posted - 03/02/2004 :  04:09:31  Show Profile  Reply with Quote
I want to replace in the string 'John Smith', I need to replace the space with nothing to get 'JohnSmith. The value char(32) needs to be replace by nothing. I am trying to get the char(32) and its replaced by value in the table but it doesn't work.
Go to Top of Page

ditch
Flowing Fount of Yak Knowledge

South Africa
1384 Posts

Posted - 03/02/2004 :  04:26:13  Show Profile  Visit ditch's Homepage  Reply with Quote
select REPLACE('John Smith', ' ', '')


Duane.
Go to Top of Page

Mathias
Posting Yak Master

France
114 Posts

Posted - 03/02/2004 :  04:40:04  Show Profile  Reply with Quote
I have other replacements to do, I know that the replace function will be used.
I need to replace
1-char(32) with ''
2-'Ltd' with 'Limited'
3-'Ltd ' with 'Limited'
and so on. The changes are kept in the above table.
When I insert ' ', '' in the table, the two fields contain ''. So later on, when I use a cursor to do all the successive replacements, it does all of them excepted the one finishing by space. The space character is trimmed and the changes 1 and 3 are not done.
Paul
Go to Top of Page

ChrisFretwell
Starting Member

Canada
43 Posts

Posted - 03/02/2004 :  11:30:43  Show Profile  Reply with Quote
You can nest the replace statements. Or write a user defined function with a bunch of if then replace's or the nested replace.

For the nesting you can just replace(replace(fielda,' ',''),'Ltd','Limited') etc.

If you have the replace on the blank space first, then you dont need to check for both 'ltd' and 'ltd '.

If you think you will need to do this for more than one field, or in more than one place then put it in a function, if its limited used, then ues the nested replace.

chris
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

USA
3245 Posts

Posted - 03/04/2004 :  14:14:37  Show Profile  Visit AjarnMark's Homepage  Reply with Quote
If you're trying to record what special character conversions are being done, instead of listing just the textual representation, you might use the ASCII number (i.e. Space = 32).

These two are equivalent
select replace('John Smith', ' ', '')
select replace('John Smith', char(32), '')

Not sure what you'd use for the second part because doing char(0) doesn't return the right result.

--------------------------------------------------------------
Find more words of wisdom at http://weblogs.sqlteam.com/markc
Go to Top of Page

Lumbago
Norsk Yak Master

Norway
2079 Posts

Posted - 03/04/2004 :  15:00:22  Show Profile  Reply with Quote
If you have alot of these replacements I think I would have made a user defined function for it.

CREATE FUNCTION dbo.MyReplace (@inText varchar(100))
RETURNS varchar(100)
AS

BEGIN
SET @inText = (SELECT REPLACE(@inText, ' ', ''))
SET @inText = (SELECT REPLACE(@inText, 'Ltd', 'Limited'))
RETURN @inText
END

and then do sort of like this:

INSERT INTO table ...
VALUES (dbo.MyReplace('John Smith Ltd'), ...)

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000 Version 3.4.03