Solved

SUBSTRING

Posted on 2014-03-18
4
183 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

733 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