Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I have table called Customer and filed names are

1. CName
2. Contact1
3. Contact2


By mistakenly we have entered two contact numbers in
Contact1
column with a "/" separator.
Ex:     CName                   Contact1                        Contact2

    1.  Sunil               985456434 / 6786877

    2.  Anil                67574989 / 990756576


Now i want to split the Contact1(985456434 / 6786877) and update it to Contact2 column.

How can i do it in SQL...?
Posted

1 solution

You need to use CHARINDEX[^] and SUBSTRING[^] function.

My variable @con1 replace with your column name ;)

SQL
DECLARE @con1 NVARCHAR(30)

SET @con1 = '67574989 / 990756576'

SELECT CHARINDEX(' ',@con1,1) AS SlashPosition

SELECT RTRIM(SUBSTRING(@con1, 1, CHARINDEX('/',@con1,1)-1)) AS Contact1,
    LTRIM(SUBSTRING(@con1, CHARINDEX('/',@con1,1)+1, LEN(@con1)-CHARINDEX('/',@con1,1))) AS Contact2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900