June 28, 2012 at 2:38 am
HI ;
How to split the csv values in rows , this is my sample string
UNIPAR LLC , IAE Power Products , MID-AM Equipment Inc.
, Diesel Electrical Equipment, Inc. , K & L Electronics Sales & Service, Inc.
, Logan Corporation , Morton Manufacturing Company , Hilliard Enterprises, Inc.
,PowerRail Distribution Inc. , Hatch & Kirk, Inc.
, Power Drives, Inc. , MAC Products, Inc. , Sterling Rail Inc. , Motive Power Resources, Inc.
, Saft America, Inc.
I want to split the comma separated vales and one condition is some of the companies having example
( Hatch & Kirk, Inc. , MID-AM Equipment Inc.) comma values with in the company name or nothing comma. we need to split the values
expected output ;
1.UNIPAR LLC
2. IAE Power Products
3. MID-AM Equipment Inc.
4. Diesel Electrical Equipment Inc. ,
5. K & L Electronics Sales & Service Inc.
6. Logan Corporation
7. Morton Manufacturing Company
8. Hilliard Enterprises Inc.
9. PowerRail Distribution Inc.
10 .Hatch & Kirk Inc.
11. Power Drives Inc.
12. MAC Products Inc.
13. Sterling Rail Inc.
14 . Motive Power Resources Inc.
15 . Saft America Inc.
any one help to solve the problem
Thanks
FA
DE
June 28, 2012 at 2:43 am
See this article, which explains how to do it --> http://www.sqlservercentral.com/articles/Tally+Table/72993/%5B/url%5D.
June 28, 2012 at 3:13 am
faijurrahuman17 (6/28/2012)
HI ;How to split the csv values in rows , this is my sample string
UNIPAR LLC , IAE Power Products , MID-AM Equipment Inc.
, Diesel Electrical Equipment, Inc. , K & L Electronics Sales & Service, Inc.
, Logan Corporation , Morton Manufacturing Company , Hilliard Enterprises, Inc.
,PowerRail Distribution Inc. , Hatch & Kirk, Inc.
, Power Drives, Inc. , MAC Products, Inc. , Sterling Rail Inc. , Motive Power Resources, Inc.
, Saft America, Inc.
I want to split the comma separated vales and one condition is some of the companies having example
( Hatch & Kirk, Inc. , MID-AM Equipment Inc.) comma values with in the company name or nothing comma. we need to split the values
expected output ;
1.UNIPAR LLC
2. IAE Power Products
3. MID-AM Equipment Inc.
4. Diesel Electrical Equipment Inc. ,
5. K & L Electronics Sales & Service Inc.
6. Logan Corporation
7. Morton Manufacturing Company
8. Hilliard Enterprises Inc.
9. PowerRail Distribution Inc.
10 .Hatch & Kirk Inc.
11. Power Drives Inc.
12. MAC Products Inc.
13. Sterling Rail Inc.
14 . Motive Power Resources Inc.
15 . Saft America Inc.
any one help to solve the problem
Thanks
FA
DE
Try out this logic in a UDF.
Use Charindex and Substring function to find the position of comma and split the statement and store splitted values in a table variable
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
🙂
June 28, 2012 at 5:31 am
Hi
You can use the following query:
DECLARE @CSV_String VARCHAR(2000)
SET @CSV_String = 'UNIPAR LLC , IAE Power Products , MID-AM Equipment Inc.
, Diesel Electrical Equipment, Inc. , K & L Electronics Sales & Service, Inc.
, Logan Corporation , Morton Manufacturing Company , Hilliard Enterprises, Inc.
,PowerRail Distribution Inc. , Hatch & Kirk, Inc.
, Power Drives, Inc. , MAC Products, Inc. , Sterling Rail Inc. , Motive Power Resources, Inc.
, Saft America, Inc.
'
;WITH CTE AS
(
SELECT 1 AS ID, SUBSTRING(@CSV_String, 1, CHARINDEX(',', @CSV_String, 1)-1) AS Col1, CHARINDEX(',', @CSV_String, 1)+1 AS START
UNION ALL
SELECT ID + 1, SUBSTRING(@CSV_String, START, ISNULL(NULLIF(CHARINDEX(',', @CSV_String, START), 0)-START, 2000)), ISNULL(NULLIF(CHARINDEX(',', @CSV_String, START), 0)+1, 0)
FROM CTE
WHERE START <> 0
)
SELECT ID, LTRIM(RTRIM(Col1)) FROM CTE
June 28, 2012 at 6:59 am
Abhijeet Chavan (6/28/2012)
HiYou can use the following query:
DECLARE @CSV_String VARCHAR(2000)
SET @CSV_String = 'UNIPAR LLC , IAE Power Products , MID-AM Equipment Inc.
, Diesel Electrical Equipment, Inc. , K & L Electronics Sales & Service, Inc.
, Logan Corporation , Morton Manufacturing Company , Hilliard Enterprises, Inc.
,PowerRail Distribution Inc. , Hatch & Kirk, Inc.
, Power Drives, Inc. , MAC Products, Inc. , Sterling Rail Inc. , Motive Power Resources, Inc.
, Saft America, Inc.
'
;WITH CTE AS
(
SELECT 1 AS ID, SUBSTRING(@CSV_String, 1, CHARINDEX(',', @CSV_String, 1)-1) AS Col1, CHARINDEX(',', @CSV_String, 1)+1 AS START
UNION ALL
SELECT ID + 1, SUBSTRING(@CSV_String, START, ISNULL(NULLIF(CHARINDEX(',', @CSV_String, START), 0)-START, 2000)), ISNULL(NULLIF(CHARINDEX(',', @CSV_String, START), 0)+1, 0)
FROM CTE
WHERE START <> 0
)
SELECT ID, LTRIM(RTRIM(Col1)) FROM CTE
Read the article that Cadavre pointed out and see why that might not be such a good idea.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply