Solved

SQL Server 2005 parse string into two column table

Posted on 2013-01-30
6
376 Views
Last Modified: 2013-01-30
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
0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 38835848
Hi,

You would have to use an sql cursor. Are you inserting the results in a table?

Giannis
0
 

Author Closing Comment

by:lrbrister
ID: 38835871
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38835872
using the function dbo.ParmsToList, code being here:
http://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

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38835881
too slow :)
0
 

Author Comment

by:lrbrister
ID: 38835891
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
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38835929
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question