Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I deconcatenate a string?

Status
Not open for further replies.

tmunson99

MIS
Jun 18, 2004
62
US
I have a table of several thousand customer names. The full customer name is in one field and I want to separate it into first name and last name. There has to be a sql command that will separate or trim a string before or after a space. I've looked at ltrim and substring, but they don't seem to do what I'm needing. Does anyone have any suggestions?
 
look for charindex() function...

select substring(yourfield,1,charindex('% %',yourfield)) as lastname

may be this is round about..there should be something easier than this...

-DNG
 
ok recently i used this function..

Code:
Create FUNCTION dbo.Split
(
    @RowData nvarchar(2000),
    @SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
    Id int identity(1,1),
    Data nvarchar(100)
) 
AS  
BEGIN 
    Declare @Cnt int
    Set @Cnt = 1

    While (Charindex(@SplitOn,@RowData)>0)
    Begin
        Insert Into @RtnValue (data)
        Select 
            Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

        Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
        Set @Cnt = @Cnt + 1
    End
    
    Insert Into @RtnValue (data)
    Select Data = ltrim(rtrim(@RowData))

    Return
END

you can just call the function...

SELECT Data FROM Split(yourfield,' ')

-DNG

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top