Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have table with a column called details,the values are separated by "|" symbol,i need to extract those data by "|" and insert into new table with fields,can anyone please help me to extract data?

VB
Name=Jhon|Age=36|Job=Sales Manager|Job Location=Texas|
Name=Tom|Age=27|Job=Sales Man|Job Location=Texas|
Name=Ferdinan|Age=38|Job=Sales Man|Job Location=Texas|
Name=Jhonson|Age=29|Job=Marketing Manager|Job Location=Texas|
Name=Mikel|Age=26|Job=Technician|Job Location=Texas|
Name=Steve|Age=25|Job=Technician|Job Location=Los Angeles|
Name=Rob|Age=29|Job=Programmer|Job Location=NC|
Posted

If SQL Server... for that particular data -- you can make it into XML and query it:

SQL
WITH [raw] AS
(
  SELECT 'Name=Jhon|Age=36|Job=Sales Manager|Job Location=Texas|' Data
UNION ALL
  SELECT 'Name=Tom|Age=27|Job=Sales Man|Job Location=Texas|' Data
UNION ALL
  SELECT 'Name=Ferdinan|Age=38|Job=Sales Man|Job Location=Texas|' Data
UNION ALL
  SELECT 'Name=Jhonson|Age=29|Job=Marketing Manager|Job Location=Texas|' Data
UNION ALL
  SELECT 'Name=Mikel|Age=26|Job=Technician|Job Location=Texas|' Data
UNION ALL
  SELECT 'Name=Steve|Age=25|Job=Technician|Job Location=Los Angeles|' Data
UNION ALL
  SELECT 'Name=Rob|Age=29|Job=Programmer|Job Location=NC|' Data
)
, [cooked] AS
(
  SELECT CAST('<Data ' + REPLACE(REPLACE(REPLACE(Data,'Job ',''),'=','="'),'|','" ') + ' />' AS XML ) Data
  FROM [raw]
)
SELECT Data.value ( '(*/@Name)[1]' , 'VARCHAR(255)' ) Name
,  Data.value ( '(*/@Age)[1]' , 'int' ) Age
,  Data.value ( '(*/@Job)[1]' , 'VARCHAR(255)' ) Job
,  Data.value ( '(*/@Location)[1]' , 'VARCHAR(255)' ) Location
FROM [cooked]


(Note that I had to remove 'Job ' to produce well-formed XML.)

http://msdn.microsoft.com/en-us/library/ms178030.aspx[^]
 
Share this answer
 
v5
See here: Converting comma separated data in a column to rows for selection[^] - it assumes a commas as the separator but that's trivial to change.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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