Solved

move to another column

Posted on 2014-02-24
7
223 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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