September 16, 2016 at 12:18 pm
Can somebody please help? Is there a fairly simple way to do this?
This is my data now.
LABORLC
152685A B C D E
152969A B
This is what I would like it to look like.
LABORLC
152685A
152685B
152685C
152685D
152685E
152969A
152969B
September 16, 2016 at 12:20 pm
erouse (9/16/2016)
Can somebody please help? Is there a fairly simple way to do this?This is my data now.
LABORLC
152685A B C D E
152969A B
This is what I would like it to look like.
LABORLC
152685A
152685B
152685C
152685D
152685E
152969A
152969B
It is not very clear what you have going on here. Are the values delimited with a space? If so then you need to look in my signature about splitting strings. If it is something else then you need to look at the first link in my signature for best practices when posting questions.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 16, 2016 at 10:02 pm
erouse (9/16/2016)
Can somebody please help? Is there a fairly simple way to do this?This is my data now.
LABORLC
152685A B C D E
152969A B
This is what I would like it to look like.
LABORLC
152685A
152685B
152685C
152685D
152685E
152969A
152969B
We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 21, 2016 at 3:09 pm
Jeff Moden (9/16/2016)
erouse (9/16/2016)
Can somebody please help? Is there a fairly simple way to do this?This is my data now.
LABORLC
152685A B C D E
152969A B
This is what I would like it to look like.
LABORLC
152685A
152685B
152685C
152685D
152685E
152969A
152969B
We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.
Sorry, here is code to generate 2 tables. I am trying to take the data from the first table (LC are grouped by LABOR and in same row) and make a new table with LC in separate row with LABOR number repeated as displayed in the second table. I cant provide what I have so far as I have no idea where to start on this!
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable
--===== Create the test table with
CREATE TABLE #mytable
(
LABOR VARCHAR(50),
LC VARCHAR(50)
)
--===== Insert the test data into the test table
INSERT INTO #mytable
(LABOR, LC)
SELECT '152736', 'A C D' UNION ALL
SELECT '152756', 'A B C D E'
--===== View the data
SELECT LABOR, LC FROM #mytable
--===== If the 2nd test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable2','U') IS NOT NULL
DROP TABLE #mytable2
--===== Create the test table with
CREATE TABLE #mytable2
(
LABOR VARCHAR(50),
LC VARCHAR(50)
)
--===== Insert the test data into the test table
INSERT INTO #mytable2
(LABOR, LC)
SELECT '152736', 'A' UNION ALL
SELECT '152736', 'C' UNION ALL
SELECT '152736', 'D' UNION ALL
SELECT '152756', 'A' UNION ALL
SELECT '152756', 'B' UNION ALL
SELECT '152756', 'C' UNION ALL
SELECT '152756', 'D' UNION ALL
SELECT '152756', 'E'
--===== View the data
SELECT LABOR, LC FROM #mytable2
September 21, 2016 at 3:16 pm
erouse (9/21/2016)
Jeff Moden (9/16/2016)
erouse (9/16/2016)
Can somebody please help? Is there a fairly simple way to do this?This is my data now.
LABORLC
152685A B C D E
152969A B
This is what I would like it to look like.
LABORLC
152685A
152685B
152685C
152685D
152685E
152969A
152969B
We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.
Sorry, here is code to generate 2 tables. I am trying to take the data from the first table (LC are grouped by LABOR and in same row) and make a new table with LC in separate row with LABOR number repeated as displayed in the second table. I cant provide what I have so far as I have no idea where to start on this!
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable
--===== Create the test table with
CREATE TABLE #mytable
(
LABOR VARCHAR(50),
LC VARCHAR(50)
)
--===== Insert the test data into the test table
INSERT INTO #mytable
(LABOR, LC)
SELECT '152736', 'A C D' UNION ALL
SELECT '152756', 'A B C D E'
--===== View the data
SELECT LABOR, LC FROM #mytable
--===== If the 2nd test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable2','U') IS NOT NULL
DROP TABLE #mytable2
--===== Create the test table with
CREATE TABLE #mytable2
(
LABOR VARCHAR(50),
LC VARCHAR(50)
)
--===== Insert the test data into the test table
INSERT INTO #mytable2
(LABOR, LC)
SELECT '152736', 'A' UNION ALL
SELECT '152736', 'C' UNION ALL
SELECT '152736', 'D' UNION ALL
SELECT '152756', 'A' UNION ALL
SELECT '152756', 'B' UNION ALL
SELECT '152756', 'C' UNION ALL
SELECT '152756', 'D' UNION ALL
SELECT '152756', 'E'
--===== View the data
SELECT LABOR, LC FROM #mytable2
Well done on the sample data! All you need is a copy of the house splitter[/url] and this is as easy as pie.
SELECT LABOR, Item
FROM #mytable
CROSS APPLY dbo.DelimitedSplit8K(LC,' ');
-- Itzik Ben-Gan 2001
September 21, 2016 at 3:59 pm
Thanks! You guys are the best!
June 21, 2017 at 5:01 pm
This was removed by the editor as SPAM
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply