Solved

SUBSTRING

Posted on 2014-03-18
4
161 Views
Last Modified: 2014-03-20
I have a column called Desc and also Hold. I would like to copy and remove "FR(space)<something>(space) from Desc to Hold.
eg.

Desc                                       Hold
FR 71B4 B3 And B5
FR B3
FR 143T SF 1.115
FR 145T

after
Desc                                        Hold
B3 And B5                           FR 71B4
                                             FR B3
SF 1.115                               FR 143T
                                             FR 145T

thanks
0
Comment
Question by:RVL
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
dannygonzalez09 earned 500 total points
Comment Utility
You can use something similar to below.. I've used variable to test the SQL .. Replace it with you column names in the update stmt and specify a condition if necessary

 DECLARE @Desc VARCHAR(50)  = 'FR 71B4 B3 And B5'

 SELECT @Desc, SUBSTRING(@Desc,CHARINDEX(' ',@Desc,4)+1,30), REPLACE(@Desc,SUBSTRING(@Desc,CHARINDEX(' ',@Desc,4)+1,30),'')

 UPDATE Table
 SET Hold = SUBSTRING(@Desc,CHARINDEX(' ',@Desc,4)+1,30), Desc = REPLACE(@Desc,SUBSTRING(@Desc,CHARINDEX(' ',@Desc,4)+1,30),'')
 WHERE <Condition>

Open in new window

0
 

Author Comment

by:RVL
Comment Utility
the only problem is i have ove 1500 records
0
 
LVL 5

Expert Comment

by:dannygonzalez09
Comment Utility
i don't see a problem with that.. if you want to test it dump your data into temp table and run the update stmt..

If you're worried about logging .. we can also have a look at using while loop to update data in batches
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
SELECT
    CASE WHEN fr_start_byte = 0
         THEN [Desc]
         ELSE LTRIM(STUFF([Desc], fr_start_byte, fr_length, ''))
    END AS New_Desc,
    CASE WHEN fr_start_byte = 0
         THEN ''
         ELSE SUBSTRING([Desc], fr_start_byte, fr_length)
    END AS Hold,
    [Desc] AS [Desc(Original)]
FROM --your_table_name_goes_here
--/* comment out "FROM (...) AS test_data" after you put in your real table name
(
    SELECT 'FR 71B4 B3 And B5' AS [Desc] UNION ALL
    SELECT 'FR B3' UNION ALL
    SELECT 'FR 143T SF 1.115' UNION ALL
    SELECT 'FR 145T'
) AS test_data
--*/
CROSS APPLY (
    SELECT CHARINDEX('FR ', [Desc]) AS fr_start_byte
) AS assign_name_to_calculated_value1
CROSS APPLY (
    SELECT CHARINDEX(' ', [Desc] + ' ', fr_start_byte + 3) - fr_start_byte AS fr_length
) AS assign_name_to_calculated_value2
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
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.

772 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

9 Experts available now in Live!

Get 1:1 Help Now