Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

SQL Server 2005 parse string into two column table

In VB I am building a string which I am passing into my stored procedure as a varchar(MAX) variable

The string looks like this

Larry,100|jane,125|Bill,75

I want to parse this into
col1      col2
Larry     100
Jane       125
Bil           75
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Larry Brister

ASKER

You joggled my memory with the "Table" comment

I remember getting some code from angelll years ago.

SELECT      MAX(CASE WHEN row_num % 2 = 1 THEN Value END) hsid,
        MAX(CASE WHEN row_num % 2 = 0 THEN Value END) hours
FROM      dbo.ParmsToList(@list, ',') d
GROUP BY(row_num - 1)/ 2
using the function dbo.ParmsToList, code being here:
https://www.experts-exchange.com/Database/Miscellaneous/A_1536-delimited-list-as-parameter-what-are-the-options.html

you can do it without a "cursor":
select left( f.value, charindex(',', f.value) - 1) col1
, substring( f.value, charindex(',', f.value) + 1, len(f.value))  col2 
  from dbo.ParmsTolist('Larry,100|jane,125|Bill,75', '|') f 

Open in new window

angelll,
I have received some great help from you for the past 10 years or so.

Usually, I just need to take a moment and look through what you gave me in the past already
Hi,

If you want (i don't know if you can) to reallocate the points, it is fine by me. After all i really didn't think that was of much help. I really expected to just start a conversation and then give you an answer.

:)

Thanks anyways,
Giannis