Solved

REPLACE or RTRIM to remove characters from a value

Posted on 2010-11-11
3
335 Views
Last Modified: 2012-05-10
I have a column with a few thousand rows that all begin with some sort of name but then end with a '#' and then a 5 or 6 digit number.

my desired result is to extract only those numbers into another column.

examples:

Doe, John and Jane #123456
Royal blue restaurant, LLC #12345

0
Comment
Question by:RustyZ32
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 34113798
update urTable
set urNewColumn = RIGHT ('urColumnName', len('urColumnName')-charindex('#', 'urColumnName') )
where charindex('#', urColumnName ) > 0
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 34113805
select substring(fieldname, charindex('#', fieldname', 0), len(fieldname))
0
 
LVL 18

Expert Comment

by:lludden
ID: 34113809
I use this function for stuff like that.

Then
SELECT dbo.Strpiece('Doe, John and Jane #123456','#',1)

CREATE FUNCTION [dbo].[StrPiece] (@String varchar(500), @Delimiter char, @Position int)  
RETURNS varchar(100) AS  
BEGIN
Declare @CurrentSection int
Declare @CurrentPosition int
Declare @SubString varchar(100)
Set @CurrentSection = 0
Set @CurrentPosition = 1
Set @Substring = ''
While @CurrentPosition <= Len(@String)
    Begin
      If Substring(@String,@CurrentPosition,1) = @Delimiter
            Set @CurrentSection = @CurrentSection + 1
      Else
             if @CurrentSection = @Position
                  Set @SubString = @Substring + Substring(@String, @CurrentPosition, 1)
      Set @CurrentPosition = @CurrentPosition + 1
    End

Return ( @Substring)
END
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

919 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

16 Experts available now in Live!

Get 1:1 Help Now