473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to replaces 1 with corresponding position

Hi everybody.

Input table and requested result:
xstring result
----------- --------
11111 12345
101011 1-3-56
1000111 1---567
Any suggestions how to write a query/UDF for replacing 1 with
corresponding position

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200808/1

Aug 20 '08 #1
3 1990
Hi everybody.
>
Input table and requested result:
xstring * * * * * result
----------- * * * *--------
11111 * * * * * *12345
101011 * * * * *1-3-56
1000111 * * * *1---567
Any suggestions how to write a query/UDF for replacing 1 with
corresponding position
Hello.

Try this:

with xstring(s) as (values
('11111')
, ('101011')
, ('1000111')
)
, t(s, rn, res) as
(
select s, 1, cast(case substr(s, 1, 1) when '1' then '1' else '-' end
as varchar(10))
from xstring
union all
select s, rn+1, res||case substr(s, rn+1, 1) when '1' then
rtrim(char(rn+1)) else '-' end
from t
where rn<length(s)
)
select t.s, t.res
from t t
join (select s, max(rn) rn from t group by s) g on t.s=g.s and
t.rn=g.rn

Sincerely,
Mark B.
Aug 21 '08 #2
CREATE TABLE InputStrings
(pattern CHAR(5) NOT NULL PRIMARY KEY,
str CHAR(5) NOT NULL );

INSERT INTO InputStrings (pattern, str)
VALUES ('11111', 'abcde'),
('11011', 'abcde'),
('11001', 'abcde'),
('00000', 'abcde'),
('01010', 'abcde'),
('10101', 'abcde');

SELECT I.pattern, I.str,
(CASE WHEN SUBSTR(I.pattern, 1, 1) = '0' THEN '-' ELSE
SUBSTR(I.str, 1, 1) END
|| CASE WHEN SUBSTR(I.pattern, 2, 1) = '0' THEN '-' ELSE
SUBSTR(I.str, 2, 1) END
|| CASE WHEN SUBSTR(I.pattern, 3, 1) = '0' THEN '-' ELSE
SUBSTR(I.str, 3, 1) END
|| CASE WHEN SUBSTR(I.pattern, 4, 1) = '0' THEN '-' ELSE
SUBSTR(I.str, 4, 1) END
|| CASE WHEN SUBSTR(I.pattern, 5, 1) = '0' THEN '-' ELSE
SUBSTR(I.str, 5, 1) END ) AS result_string
FROM InputStrings AS I
Aug 21 '08 #3
Thank you very much.
It is working perfect.

4.****@mail.ru wrote:
>Hi everybody.
[quoted text clipped - 6 lines]
>Any suggestions how to write a query/UDF for replacing 1 with
corresponding position

Hello.

Try this:

with xstring(s) as (values
('11111')
, ('101011')
, ('1000111')
)
, t(s, rn, res) as
(
select s, 1, cast(case substr(s, 1, 1) when '1' then '1' else '-' end
as varchar(10))
from xstring
union all
select s, rn+1, res||case substr(s, rn+1, 1) when '1' then
rtrim(char(rn+1)) else '-' end
from t
where rn<length(s)
)
select t.s, t.res
from t t
join (select s, max(rn) rn from t group by s) g on t.s=g.s and
t.rn=g.rn

Sincerely,
Mark B.
--
Message posted via http://www.dbmonster.com

Aug 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kalbee | last post by:
When we insert text into field in a table, SQL SERVER apparently replaces apostrophes with question marks -- is there a way to not have this occur? We don't have this happen with the mySQL...
2
by: Harald Armin Massa | last post by:
again, and again ... another try of templating txt=""" <html> <body> <p>whatever</p> <!--tree--> <p>the machine with bing</p> <!--house--> <p>10% of boo is foo</p>
1
by: Rob Smegma | last post by:
Greetings, I have an XML document like so: <root> <head> <meta name=1/> <meta name=2/> <meta name=3/> </head>
8
by: David | last post by:
As very often is the case a web page has a main, dynamic content page and a surrounding, more constant "frame" (not html frame!) for stuff like menu, logo, links etc. In my old ASP web pages I...
4
by: Jim in Arizona | last post by:
This doesn't make any sense to me. I'm hoping some SQL guru out there knows the answer. I'm making a help desk type database system. There's only two tables, one called TTickets and the other...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
0
by: Rob R. Ainscough | last post by:
Dim slt(0) As Byte Dim pdb As New PasswordDeriveBytes(strKey24, slt) Dim bytDerivedKey() As Byte = pdb.GetBytes(24) VS 2005 issues a warning for using .GetBytes() -- the exact warning: ...
8
by: tomasio | last post by:
Dear NG-Readers, I forgot to post the URL to the html-file and the linked CSS. Here they are: html: http://tomasio.at/temp/Unterseite_Templ2.html CSS: http://tomasio.at/temp/stylesheet.css...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.