Solved

move to another column

Posted on 2014-02-24
7
222 Views
Last Modified: 2014-03-07
Hi,

i would like to move one size to another column based on the word "ID"

eg
before

size
0.3750" ID x 0.8750" OD x 0.2188" WD

after
size                                                              attrib1
0.8750" OD x 0.2188" WD                         0.3750" ID

Thanks
0
Comment
Question by:RVL
[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
  • +1
7 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39883021
A few questions. Does the portion with the "ID" value always come at the beginning or not?
Is it always followed by the "x" which gets eliminated in the transformation? Does the column attrib1 already exist? Can "ID" occur more than once? Perhaps some sample data that includes the various criteria and the expected final results would be helpful.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39883023
try this out

DECLARE @T VARCHAR(1000)
SET @T = '0.3750" ID x 0.8750" OD x 0.2188" WD'

select @T

select substring(@t,charindex('ID x',@T) + len('ID x'),len(@t)-(charindex('ID x',@T) + len('ID x'))), substring(@t,0,charindex('ID x',@T) + len('ID x'))

Open in new window

0
 

Author Comment

by:RVL
ID: 39883049
it's not always in the beginning and may not always follow by a X and yes attrib1 exists
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 32

Expert Comment

by:awking00
ID: 39883084
So can I assume the x stands for "times" and, if the ID measurement is the last, it wouldn't be followed by the x. Conversely, if ID is in the middle, it would be both preceded and succeded by the x. The next question is, "Do all of the measurements take the same format?" In your example, they all begin with 0 followed by a decimal point and four digits then a double quote (I assume stands for inches).
0
 

Author Comment

by:RVL
ID: 39883100
yes depends were the ID is it could only be 0.3750" ID in the column, could have 12.3750" ID
or 7 MM ID
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 250 total points
ID: 39883131
in order to achieve this without a complicated substrings..
You have to create the below split function

CREATE FUNCTION dbo.Split
(
 @RowData nvarchar(2000),
 @SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
 Id int identity(1,1),
 Data nvarchar(100)
)
AS
BEGIN
 Declare @Cnt int
 Set @Cnt = 1
 DECLARE @index INT
 SET @index = Charindex(@SplitOn,@RowData)
 While (@index>0)
 Begin
 Insert Into @RtnValue (data)
  Select
 Data = ltrim(rtrim(Substring(@RowData,1,@index-1)))
 
 Set @RowData = Substring(@RowData,@index+1,len(@RowData))
 Set @Cnt = @Cnt + 1
 SET @index = Charindex(@SplitOn,@RowData)
 End
 
 Insert Into @RtnValue (data)
 Select Data = ltrim(rtrim(@RowData))
 
 Return
END

Open in new window


and then you can achieve what you are looking for by using the below code

DECLARE @T VARCHAR(1000)
DECLARE @A VARCHAR(1000), @B VARCHAR(1000)

SET @T = '0.3750" ID x 0.8750" OD x 0.2188" WD'
SET @T = '0.8750" OD x 0.3750" ID x 0.2188" WD'

;WITH C AS
(select *,RIGHT(LTRIM(RTRIM(DATA)),2) T FROM dbo.split(@T,'x'))
SELECT @A = (SELECT DATA FROM C WHERE T = 'OD') + ' x ' + (SELECT DATA FROM C WHERE T = 'WD')
;WITH C AS
(select *,RIGHT(LTRIM(RTRIM(DATA)),2) T FROM dbo.split(@T,'x'))
SELECT @B = (SELECT DATA FROM C WHERE T = 'ID')

SELECT @A, @B

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39883459
I strongly prefer CROSS APPLYs to a separate multi-line function.  Functions like that are very hard on performance, esp. in this case as the function itself has a loop.

The code below picks up any string, delimited by a space, that appears before " ID".  The really tricky part using this method is if extra spaces appear between the value and " ID".  The PATINDEX handles that part, to make sure those spaces get included in the ID value (the extra spaces can also be removed from the result string if you prefer).


SELECT
    size,
    --id_end, id_length,    
    LTRIM(SUBSTRING(size, id_end - id_length, id_length)) AS attrib1,
    STUFF(size, id_end - id_length + 1, id_length + CASE WHEN SUBSTRING(size, id_end, 2) = ' x' THEN 2 ELSE 0 END, '') AS size    
FROM (
    SELECT '0.3750" ID x 0.8750" OD x 0.2188" WD' AS size UNION ALL
    SELECT '0.8750" OD x 0.3750" ID x 0.2188" WD' UNION ALL
    SELECT '0.8750" OD x 0.2188" WD 0.30" ID  ' UNION ALL
    SELECT '0.3750"    ID x 0.8750" OD x 0.2188" WD' UNION ALL
    SELECT '0.8750" OD x 0.3750"      ID x 0.2188" WD' UNION ALL
    SELECT '0.8750" OD x 0.2188" WD 0.30"     ID other chars'    
) AS test_data
CROSS APPLY (
    SELECT CHARINDEX(' ID', size) + 3 AS id_end
) AS cross_apply_1
CROSS APPLY (
    SELECT REVERSE(LEFT(size, id_end - 4)) AS id_segment
) AS cross_apply_2
CROSS APPLY (
    SELECT CHARINDEX(' ', id_segment + ' ', PATINDEX('%[^ ]%', id_segment)) + 3 AS id_length
) AS cross_apply_3
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

739 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