473,236 Members | 1,292 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,236 software developers and data experts.

Parsing a varchar(79) column

Hi,

I have a varchar column with values like:

000 BNP=Item one HOP=Item two LLT=Item three
001 LLT=Item one
002 BNP=Item one LLT=Item two

I want to parse this rows into a table similar to the following output:

000 BNP=Item one
000 HOP=Item two
000 LLT=ItemThree
001 LLT=Item one
002 BNP=Item one
002 LLT=Item two

Has anybody done a similar assignment? Can you share me your solution?

Thank you much!

Edgar J.

May 18 '06 #1
4 1184
SQL
>>Has anybody done a similar assignment? Can you share me your solution?

Is this homework??

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 18 '06 #2
No, it's a project for my company.

May 18 '06 #3
Hi,

Resolved on my own with a slight twist on the output.

Here is the code, just in case you may have a similar user request
someday.

declare @txt varchar(80)
declare @delimchar nchar(1)
set @delimchar = '='
declare @delimPos1 int, @delimPos2 int,@delimPos3 int
set @txt = 'BNR=First item KKR=Second item CNC=Third item '
set @delimPos1 = charindex(@delimChar,@txt,1)
set @delimPos2 = charindex(@delimChar,@txt,@delimPos1 + 1)
set @delimPos3 = charindex(@delimChar,@txt,@delimPos2 + 1)

select line1=case
when @delimpos1 = 4 then substring(@txt,1,@delimPos2 -4)
else substring(@txt,1,80)
end,
line2 =case
when @delimpos2 > 0 then substring(@txt,@delimPos2
-3,(@delimPos3 -4) - (@delimPos2 -3))
else null
end,
line3 = case
when @delimpos3 > 0 then substring(@txt,@delimPos3 -3,80 -
(@delimPos3 -2))
else null
end
go
Output will look like this:
Line1 Line2 Line3
BNR=First item KKR=Second item CNC=Third item

May 18 '06 #4
Google some old postings here about spliting comma separated lists. I
have a signle query version out there that uses a sequence table and no
proceudral code.

However, any typo will screw up things and you still need to go back
and fix the real problem -- lack of proper column constraints! Mop the
floor and fix the leak! Do not let this crap in your database again.

May 19 '06 #5

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

Similar topics

6
by: Bill | last post by:
In an effort to improve the speed of queries against my main table, I'll be indexing a column whose data type is varchar(50). Would I be better off (better performance) if I changed the column's...
4
by: blue | last post by:
Is there a preferred way to order a varchar column numerically (for those that are numeric), then alphanumerically for all others? I've tried: ORDER BY CASE WHEN IsNumeric(<column_name>) = 1...
2
by: Alexandre H. Guerra | last post by:
I needed to log all statements executed during a period of time and now i need ordering the long varchar column in the statements monitor table (STMT_TEXT) Is there any flag to set to release...
2
by: Laurent | last post by:
DB2 8.1 ------- db2 => create table test (COL1 VARCHAR(10)) db2 => insert into test values ('A') db2 => insert into test values ('A ') db2 => insert into test values ('B') db2 => insert into...
5
by: John Sidney-Woollett | last post by:
Is it possible to alter a table to resize a varchar column? The PG docs indicate lots of uses for "alter table T alter column C..." but not one that allows the changing of the type (or same type,...
1
by: RGow | last post by:
Hi, I'd like to know whether there is a way to alter the data type of a VARCHAR FOR BIT DATA column to be simply a VARCHAR column? I executed an ALTER TABLE statement, similar to, ALTER TABLE...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
3
by: Okonita | last post by:
Hi everyone, I have been building a set of tables for a new database and having problem with tables having columns defined as Long Varchar. Can someone look this over and tell me what I am doing...
0
by: pkrulz | last post by:
Hi I have a varchar column containing a list of numbers separated by commas. eg ColName Type Data NumberList Varchar(50) 15,1,14,13 ...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.