<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
        {font-family:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:241455954;
        mso-list-type:hybrid;
        mso-list-template-ids:2073467786 134807567 134807577 134807579 134807567 134807577 134807579 134807567 134807577 134807579;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hi All,<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">On Ingres 11<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I have just had a request to take a table with a long varchar field which is formatted by a third party to be &#8216;tag1:value1|tag2:value2|&#8230;.|tagN:valueN&#8217; and generate a query which will have the tag names as column headings and the values
 as indicated.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">&#8230;Yikes&#8230;<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">The only thing we are given:<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><![endif]>Although specified as a long varchar in reality no record is over 550 characters.<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><![endif]>each record will have the same tags in the same order, but there may be no associated value.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">So far I can&#8217;t do this in a single simple query, the only thing I&#8217;ve got is to do a copy out copy in as follows:<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">declare global temporary table stuff as select varchar(stupid_long, 1000) as shorter_string<o:p></o:p></p>
<p class="MsoNormal">from &#8230; on commit preserve rows with norecovery;<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">copy stuff(shorter_string=c0nl) into 'shorter_string.dat';<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">declare global temporary table stuff2(<o:p></o:p></p>
<p class="MsoNormal">tag1varchar(32) not null with default, /* Just a guess on field width, it looks about right&#8230;*/<o:p></o:p></p>
<p class="MsoNormal">tag2 varchar(32) not null with default,<o:p></o:p></p>
<p class="MsoNormal">&#8230;<o:p></o:p></p>
<p class="MsoNormal">tagN varchar(32) not null with default<o:p></o:p></p>
<p class="MsoNormal">) on commit preserve rows with norecovery;<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">copy stuff2(<o:p></o:p></p>
<p class="MsoNormal">dummy='d0:', tag1 = 'c0|',<o:p></o:p></p>
<p class="MsoNormal">dummy='d0:', tag2 = 'c0|',<o:p></o:p></p>
<p class="MsoNormal">&#8230;<o:p></o:p></p>
<p class="MsoNormal">dummy='d0:', tagN = c0nl<o:p></o:p></p>
<p class="MsoNormal">) from &#8216;shorter_string.dat';<o:p></o:p></p>
<p class="MsoNormal">\p\g<o:p></o:p></p>
<p class="MsoNormal">select first 20 * from stuff2;<o:p></o:p></p>
<p class="MsoNormal">\p\g<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">That seems to work, but having taken it out of the database onto the file system I could have just written a Perl program to do the deed and the user (God bless their little cotton socks) doesn&#8217;t want that.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">The only other thing I can think of is a raging great statement with lots of locates and lengths and substr statements.<o:p></o:p></p>
<p class="MsoNormal">select first 10<o:p></o:p></p>
<p class="MsoNormal">cast(substr(shorter,<o:p></o:p></p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp; locate(shorter, 'tag1:') &#43; length('tag1:'),<o:p></o:p></p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp; locate(shorter, 'tag2:') - locate(shorter, 'tag1:') - length(&#8216;tag1:') - 1<o:p></o:p></p>
<p class="MsoNormal">) as varchar(32)) as tag1,<o:p></o:p></p>
<p class="MsoNormal">&#8230;<o:p></o:p></p>
<p class="MsoNormal">from (select varchar(stupid_long, 1000) as shorter from &#8230;) x<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Anyone got a better idea?<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Martin Bowes<o:p></o:p></p>
</div>
</body>
</html>