Showing posts with label substrings. Show all posts
Showing posts with label substrings. Show all posts

Wednesday, March 28, 2012

Replacements of substrings in strings

This is not a piece of cake as I thought.

Have to replace few characters with few other characters in the string.

Now, I am using nested Replace:

(Replace(Replace(MyString,'','UE'),'','OE')

This example is simplified, number of replacements is more than eight of them.

Is there any other, more elegant, way to do this in just one command?No, but if you find yourself doing the same REPLACE in several places in your code, you can roll it into a function for ease of programming and clarity.|||Please ealborate a little...first read the sticky at the top of the forum...but my guess is you need to fix a table in one shot...

I would write a view, unload it, then reload the table...

Just a guess though

I hate DB2 OS/390

Monday, March 26, 2012

REPLACE SUBSTRING BETWEEN TWO CHARACTERS

Hi.
I have to replace substrings which are between two characters.
For example: I have to replace or remove all the characters that are
between ' \':
So, i have the string abcde\fgh\ikl and i want to get: abcdeikl.
I use the following select statement:
REPLACE(ColumnName, SUBSTRING([ColumnName, CHARINDEX('\', ColumnName),
CHARINDEX('\', ColumnName, CHARINDEX('\', ColumnName) + 1) -
CHARINDEX('\', ColumnName + 1), '')
Is there any more efficient way?
Thanks.Hi
declare @.d varchar(50)
set @.d='abkcde\fgh\ikl'
select STUFF(@.d,start,endpos-start,'')
from
(
select charindex('',@.d,1) as start,
len(@.d)-charindex('',reverse(@.d),1)+2 as endpos
) as der
<stelioshalkiotis@.yahoo.gr> wrote in message
news:1132833870.358155.145790@.g47g2000cwa.googlegroups.com...
> Hi.
> I have to replace substrings which are between two characters.
> For example: I have to replace or remove all the characters that are
> between ' ':
> So, i have the string abcde\fgh\ikl and i want to get: abcdeikl.
> I use the following select statement:
> REPLACE(ColumnName, SUBSTRING([ColumnName, CHARINDEX('', ColumnName),
> CHARINDEX('', ColumnName, CHARINDEX('', ColumnName) + 1) -
> CHARINDEX('', ColumnName + 1), '')
> Is there any more efficient way?
> Thanks.
>|||or
declare @.d varchar(50)
set @.d='abkcde\fgh\ikl'
Select
Substring(@.d,1,charindex('\',@.d)- 1)+reverse(Substring(reverse(@.d),1,chari
nde
x('\',reverse(@.d))-1))
Madhivanan