Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SUBSTRING

Posted on 2014-03-18
4
Medium Priority
?
187 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 2000 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 70

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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