Solved

move to another column

Posted on 2014-02-24
7
210 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
it's not always in the beginning and may not always follow by a X and yes attrib1 exists
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
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:
ScottPletcher earned 250 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now