Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SUBSTRING

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

971 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