dbTalk Databases Forums  

Parse sql values into columns

microsoft.public.sqlserver.tools microsoft.public.sqlserver.tools


Discuss Parse sql values into columns in the microsoft.public.sqlserver.tools forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Smith
 
Posts: n/a

Default Parse sql values into columns - 01-05-2010 , 05:03 PM






Hello,

I have a sql 2000 table that has a column with values such as

First Name - George Last Name - Smith Room number - 229 Chart Location
- 7FRH.2
First Name - Mark Last Name - Johnson Room number - 976 Chart Location
- 4FV.5
First Name - Mary Last Name - Wesley Room number - 224 Chart Location
- 8BSS.3
First Name - Lucia Last Name - Guess Room number - 453 Chart Location
- 6BF.1
First Name - Charlotte Last Name - Lopez Room number - 229 Chart
Location - 4VW.5

I need to parse these values into columns. From these columns I only
need the actual data and not the delimiters

the delimeters are:

"First Name - "
" Last Name - "
" Room number - "
" Chart Location - "

Therefore, I need to get 4 columns:

George Smith 229 7FRH.2
Mark Johnson 976 4FV.5
and so on..

Any ideas on how to parse these values into the 4 columns I need?

Thanks!

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Parse sql values into columns - 01-06-2010 , 04:45 AM






John Smith (ILCSP (AT) NETZERO (DOT) NET) writes:
Quote:
I have a sql 2000 table that has a column with values such as

First Name - George Last Name - Smith Room number - 229 Chart Location
- 7FRH.2
First Name - Mark Last Name - Johnson Room number - 976 Chart Location
- 4FV.5
First Name - Mary Last Name - Wesley Room number - 224 Chart Location
- 8BSS.3
First Name - Lucia Last Name - Guess Room number - 453 Chart Location
- 6BF.1
First Name - Charlotte Last Name - Lopez Room number - 229 Chart
Location - 4VW.5

I need to parse these values into columns. From these columns I only
need the actual data and not the delimiters

the delimeters are:

"First Name - "
" Last Name - "
" Room number - "
" Chart Location - "

Therefore, I need to get 4 columns:

George Smith 229 7FRH.2
Mark Johnson 976 4FV.5
and so on..

Any ideas on how to parse these values into the 4 columns I need?
Looks like you need to spend some time on reading about the string
functions in T-SQL, particularly charindex and substring.

But unless the fields infomation were machine-generated, there is a
fair chance that not all columns are equally formatted, but you will
need a lot of intelligence in the code to sort this out. In that case,
T-SQL is definitely not the best tool, but I think you are better of
using a client language, preferrably one with good support for regular
expressions.

I don't know who made that table design from the beginning, but the
above is certainly not how you should store data in a relational database.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #3  
Old   
John Smith
 
Posts: n/a

Default Re: Parse sql values into columns - 01-06-2010 , 09:16 AM



Hi Erl,

Thanks for the reply. I know I need to immerse myself into reading
more SQL and string functions, but right now I need to solve this
problem. I am still trying, but I can't find a solution for it.

Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Parse sql values into columns - 01-06-2010 , 09:42 AM



John Smith (ILCSP (AT) NETZERO (DOT) NET) writes:
Quote:
Thanks for the reply. I know I need to immerse myself into reading
more SQL and string functions, but right now I need to solve this
problem. I am still trying, but I can't find a solution for it.
Something on the line of:

SELECT substring (col, charindex(@label1, col) + len(@label1),
charindex(@label2, col) -
(charindex(@label1, col) + len(@label1))
FROM tbl

That is is only one column, repeat for the next.

The above is just an outline. There is a tedious work to test that
all boundaries correct.

And, yes, it's quite boring code to write.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.