Solved

move to another column

Posted on 2014-02-24
7
218 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

776 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