Solved

SUBSTRING

Posted on 2014-03-18
4
184 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
[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
4 Comments
 
LVL 5

Accepted Solution

by:
dannygonzalez09 earned 500 total points
ID: 39937467
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
ID: 39937526
the only problem is i have ove 1500 records
0
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39937637
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:Scott Pletcher
ID: 39938373
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

Technology Partners: 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!

Question has a verified solution.

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

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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