Click here to Skip to main content
16,013,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Table name SOURCE in Database with 3 columns name as From_Place, To_Place, Area.

From_Place Consist the name of Source Place Name

To_Place consist the name of Destination Plce Name

Area consist both source and destinatio place seprated by using 'to' keyword.

All i want to seprate data(Source Place Name and Destination Place Name) from column name 'Area' and want to insert into column From_Place and To_Place.

Help me to Sort Out this Problem????????


Thanks in Advance.
Posted
Comments
gvprabu 10-May-13 3:49am    
sure we will help you... give some sample data. then only its easy for us to give the right solution.
ashu_dhiman 10-May-13 4:50am    
Currently Data Store in Table like...

From_Place TO_Place Area
London Scottloand NULL

New Delhi Mumbai NULL

NULL NULL Kolkata To Chennai

NULL NULL Chandigarh TO Dehradun


Required Result is to be Like

From_Place TO_Place Area
London Scottloand NULL

New Delhi Mumbai NULL

Kolkata Chennai NULL

Chandigarh Dehradun NULL
gvprabu 10-May-13 4:03am    
See split the values is simple using CHARINDEX and SUBSTRING functions. But the problem is if any where in your Source Place having "TO" String then 100% your Split process goes wrong.

1 solution

Hi,

Check the following Script...
SQL
SELECT COALESCE(From_Place,SUBSTRING(Area,1,CHARINDEX(' TO ', Area, 1))) 'From_Place',
COALESCE(To_Place,SUBSTRING(Area,CHARINDEX(' TO ', Area, 1)+4,LEN(Area))) 'To_Place',
NULL Area
FROM SOURCE 

Regards,
GVPrabu
 
Share this answer
 
Comments
Maciej Los 10-May-13 6:06am    
+5
gvprabu 10-May-13 6:09am    
Thanks Maciej Los
ashu_dhiman 10-May-13 7:07am    
Thank You..... :)

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