Solved

REPLACE or RTRIM to remove characters from a value

Posted on 2010-11-11
3
334 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
Comment Utility
update urTable
set urNewColumn = RIGHT ('urColumnName', len('urColumnName')-charindex('#', 'urColumnName') )
where charindex('#', urColumnName ) > 0
0
 
LVL 11

Expert Comment

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

Expert Comment

by:lludden
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

18 Experts available now in Live!

Get 1:1 Help Now