tsql - How to pivot a table to a view on matching-length delimited cells -


Disclaimer: I am working with an old legacy system, so any comments I have made about poor design are meaningless. , Although I really appreciate this kind of feeling a new version that solves most legacy problems, but we still have to retain the old system, so basically, we have to manage now .


I have a table that looks like this (yes, it is a single column, I know):

and I need to see a report (which is for the reporting purpose) which is dynamically said in the table Process the data and it will return:

desired view

Values ​​\ n- Delimited (shuddering) and you can always assume that each In the same number of values ​​in El (9 example, although maybe 4 or 12 or any number of other databases), but I think that could not be hurt no entry in the event of missing values. They will always be in a matching order (for example, 'AUD', 'Australian Dollar', and '$' are the first values ​​in their respective cells, and so on).

I have discovered different ways to divide a single cell into one view, but nothing is the way I need it, as if the data should be merged. Sitting at home with cold has not helped my research capabilities. Help me over stack overflow, you are my only hope!

Bonus points for streamlined, relatively readable SQL instances, although I am anticipating disturbance as a natural sub-product of the disturbing nature of my essential solution. / P>

In this way, I did not take the time to prepare the tables, but it should be quite clear that How can you change your variables with your rows You also want to move a transfered four (10) where I have used a comma, you can compile a valuable function in the table and then call it as a sequence can do. Announce @ xml1xml @ xml1xml Announce @ xml2xml Announce @xml3xml @ c1 nvarchar (250) Declared @ C2 Navarre (250) Announced @C3Neave (250) set @ C1 = N 'AUD, CAD, Euro, GBP, JPY, NJD, USD, KES, SHF'; Set @ C2 = N'Australian Dollar, Canadian Dollar, Euro, Pound Sterling, Yen, New Zealand Dollar, United States Dollar, Kenyan Shilling, Swiss Franc '; Set @ c3 = n '$, $, c, l, y, $, $, k, f'; - You set the / n delimited code @ xml1 = N '& lt; Root & gt; & Lt; R> Replace (@ c1, four (10), ' r + 'Replaced (@ C3,', ',' r & gt; ') As the [code] in the form of [code] as the [code] & lt; / root & gt; 'ck, c.value ('. ',' Varchar (max) ') Code as code (code), name.name, symbol.symbol (more than ROW_NUMBER) (sequence by @@ rowcount) as a code (c) as xml1.nodes ('/ / root / r') Include internal (select ROW_NUMBER () @ n (n / a / root / r), as nk, n.value ('.', 'Varchar (max)' as [name] from @ xml2.nodes ('/ / root / r ') As a (N) form as a name on code.ck = name.nk (select ROW_NUMBER ()) (@@ Rowcount) as the symbol in the form of SP As the symbol from [symbol] @ xml3.nodes ('/ / root / r') (s)), as s.value ('.', 'Varchar (max)'). = Name.nk

You can work as a single script in SSMS for verification, no schema is required.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -