PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > Substring Query for data between special characters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Substring Query for data between special characters

Réponse
 
LinkBack Outils de la discussion
Vieux 14/06/2008, 05h56   #1
Allan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Substring Query for data between special characters

I have a field in SQL that contains email messages. I would like to extract
the FROM, SUBJ, and TO header data from the field. Here is an example:

Date: Thu, 23 Mar 2000 10:01:33 +0000^^From: John Doe abc@abc.com^^Subject:
Fun Time^^To: Jane Doe ldef@def.com^^

The each carat character represents a square cariage return character
char(13).

I know I can use the following quesy to extract data between two char(13)
characters:

select
SUBSTRING([rfc822],CHARINDEX('char(13)',rfc822),CHARINDEX('char(13)' ,[rfc822])) as col01
from mailbox

But since there are two char(13)'s in a row, I get no data. How can I create
this query so that I get the data between the double char(13)s.

Something like this:

select
SUBSTRING([rfc822],CHARINDEX('char(13)','char(13)',rfc822),CHARINDEX ('char(13)'',char(13)',[rfc822])) as col01
from mailbox

Thanks in advance.

Allan

  Réponse avec citation
Vieux 14/06/2008, 17h03   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Substring Query for data between special characters

This type of problem is best suited for environment that supports regular
expressions. If using SQL Server 2005 take a look at CLR and how you can use
regular expressions:
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
http://blogs.msdn.com/sqlclr/archive.../29/regex.aspx

Here is one way to parse the string in T-SQL. In the code below replace the
reference to the system table master..spt_values with real auxiliary table
with numbers (http://www.projectdmx.com/tsql/tblnumbers.aspx).

DECLARE @string VARCHAR(500);

SET @string =
'Date: Thu, 23 Mar 2000 10:01:33 +0000' + CHAR(13) + CHAR(13) +
'From: John Doe abc@abc.com' + CHAR(13) + CHAR(13) +
'Subject: Fun Time' + CHAR(13) + CHAR(13) +
'To: Jane Doe ldef@def.com' + CHAR(13) + CHAR(13);

SELECT MAX(CASE WHEN line_idx = 2 THEN line END) AS email_from,
MAX(CASE WHEN line_idx = 3 THEN line END) AS email_subject,
MAX(CASE WHEN line_idx = 4 THEN line END) AS email_to
FROM (
SELECT SUBSTRING(@string, n, CHARINDEX(CHAR(13) + CHAR(13), @string +
CHAR(13) + CHAR(13), n) - n ) AS line,
n + 1 - LEN(REPLACE(LEFT(@string, n), CHAR(13) + CHAR(13), ' '))
AS line_idx
FROM (SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND 500) AS Nums(n)
WHERE SUBSTRING(CHAR(13) + CHAR(13) + @string, n, 2) = CHAR(13) + CHAR(13)
AND n < LEN(@string) + 1) AS T;

Results:

email_from email_subject email_to
---------------- -------------------- --------------------
From: John Doe abc@abc.com Subject: Fun Time To: Jane Doe ldef@def.com

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 15/06/2008, 01h54   #3
Allan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Substring Query for data between special characters

Plamen,

Thank you for your reply. I did like the article regarding a numbers table,
that can be usefyl. Unfortunately most of the answer you provided is beyond
my humble beginer's skills. I'm not even sure where to run a T-SQL quesry
because you can't run it from the SQL Table Query window.

I did not realize how complex an issue this was. I was hoping to be able to
create a query I could run on and ASP.net ASPX page in a datagrid or in SQL
Server and create a View based on the Query.

Thank you for your though.

Any thoughts on how I can do this in ASP.net or a SQL View?

Allan

__________________________________________________ ___________

"Plamen Ratchev" wrote:

> This type of problem is best suited for environment that supports regular
> expressions. If using SQL Server 2005 take a look at CLR and how you can use
> regular expressions:
> http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
> http://blogs.msdn.com/sqlclr/archive.../29/regex.aspx
>
> Here is one way to parse the string in T-SQL. In the code below replace the
> reference to the system table master..spt_values with real auxiliary table
> with numbers (http://www.projectdmx.com/tsql/tblnumbers.aspx).
>
> DECLARE @string VARCHAR(500);
>
> SET @string =
> 'Date: Thu, 23 Mar 2000 10:01:33 +0000' + CHAR(13) + CHAR(13) +
> 'From: John Doe abc@abc.com' + CHAR(13) + CHAR(13) +
> 'Subject: Fun Time' + CHAR(13) + CHAR(13) +
> 'To: Jane Doe ldef@def.com' + CHAR(13) + CHAR(13);
>
> SELECT MAX(CASE WHEN line_idx = 2 THEN line END) AS email_from,
> MAX(CASE WHEN line_idx = 3 THEN line END) AS email_subject,
> MAX(CASE WHEN line_idx = 4 THEN line END) AS email_to
> FROM (
> SELECT SUBSTRING(@string, n, CHARINDEX(CHAR(13) + CHAR(13), @string +
> CHAR(13) + CHAR(13), n) - n ) AS line,
> n + 1 - LEN(REPLACE(LEFT(@string, n), CHAR(13) + CHAR(13), ' '))
> AS line_idx
> FROM (SELECT number
> FROM master..spt_values
> WHERE type = 'P'
> AND number BETWEEN 1 AND 500) AS Nums(n)
> WHERE SUBSTRING(CHAR(13) + CHAR(13) + @string, n, 2) = CHAR(13) + CHAR(13)
> AND n < LEN(@string) + 1) AS T;
>
> Results:
>
> email_from email_subject email_to
> ---------------- -------------------- --------------------
> From: John Doe abc@abc.com Subject: Fun Time To: Jane Doe ldef@def.com
>
> HTH,
>
> Plamen Ratchev
> http://www.SQLStudio.com
>

  Réponse avec citation
Vieux 15/06/2008, 04h41   #4
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Substring Query for data between special characters

You run queries from a new Query window in SQL Server 2005, and Query
Analyzer in SQL Server 2000. But since you do not feel comfortable with
T-SQL you should look to solve this problem in .NET. The RegEx class in .NET
is very powerful tool and you can write regular expressions to extract the
different segments from the text. That will be easier and more efficient.
Here are a couple articles on using regular expressions in .NET:
http://msdn.microsoft.com/en-us/library/ms972966.aspx
http://aspnet.4guysfromrolla.com/articles/022603-1.aspx

Here is how that query can be used in a view (I used the table name and
column you provided in the initial post):

CREATE VIEW EmailSplit
AS
SELECT MAX(CASE WHEN line_idx = 2 THEN line END) AS email_from,
MAX(CASE WHEN line_idx = 3 THEN line END) AS email_subject,
MAX(CASE WHEN line_idx = 4 THEN line END) AS email_to
FROM (
SELECT SUBSTRING(rfc822, n, CHARINDEX(CHAR(13) + CHAR(13), rfc822 + CHAR(13)
+ CHAR(13), n) - n ) AS line,
n + 1 - LEN(REPLACE(LEFT(rfc822, n), CHAR(13) + CHAR(13), ' ')) AS
line_idx
FROM (SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND 500) AS Nums(n)
CROSS JOIN Mailbox
WHERE SUBSTRING(CHAR(13) + CHAR(13) + rfc822, n, 2) = CHAR(13) + CHAR(13)
AND n < LEN(rfc822) + 1) AS T;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 01/07/2008, 20h50   #5
Ratandeep Gupta
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Extract substring from a file in sql 2005

I have a file in database that I would use to extract some data from. The file is having data like:

GRAND SUMMARY: PART 1

ACC-1-ACC-OP - 2|ACC-8-ACC-OP- 1|AHA-1-AHA-OP- 90|
AJS-22-QWS-OP- 6|ASS-20-QHQ-90- 2| AGA-22-AHA-OP-90|
..
..
..
AJS-22-QWS-OP- 6|ASS-20-QHQ-90- 2| AGA-22-AHA-OP-90|
AJS-22-QWS-OP- 6|ASS-20-QHQ-90- 2|

I want all the fields to be separately extracted as like:
1. ACC-1-ACC-OP - 2
2. ACC-8-ACC-OP- 1
3. AHA-1-AHA-OP- 90
4. AJS-22-QWS-OP- 6
5. ASS-20-QHQ-90- 2
6.
7.

An entry is completed when there is a "|" character. I am new to sql so if you can explain the code as well in little detail, I would really appreciate it.

thanks!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12656 seconds with 13 queries