Solved

move to another column

Posted on 2014-02-24
7
216 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

863 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

27 Experts available now in Live!

Get 1:1 Help Now