how to extract an email from the FROM field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinikatyal
    New Member
    • Oct 2011
    • 1

    how to extract an email from the FROM field

    What i want to do is extract an email from the FROM field which is stored in the following format
    "vini katyal" <vinikatyal@yah oo.com> varchar field
    Last edited by NeoPa; Oct 1 '11, 11:53 AM. Reason: We don't allow shouting where all, or nearly all, of a post is in bold
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    I take it that you're looking to do this via a simple SELECT query?

    Pat

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      You need to parse that column. What do you have so far?



      ~~ CK

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        You could try something like this. It assumes that you have a table of digits (tblDigits) from 1 to 100, under a column name digit.

        Code:
        SELECT SUBSTRING(tblContacts.fldEmail, tA.digit + 1, tB.digit - tA.digit - 1)
        FROM tblContacts, tblDigits tA, tblDigits tB
        WHERE tblContacts.fldEmail IS NOT NULL
              AND
              tB.digit - tA.digit > 0
              AND
              tB.digit - tA.digit < LEN(tblContacts.fldEmail)
              AND
              SUBSTRING(tblContacts.fldEmail, tA.digit, 1) = '<'
              AND
              SUBSTRING(tblContacts.fldEmail, tB.digit, 1) = '>';

        I'm sure there are other possibilities.

        Pat

        Comment

        • clvandyke
          New Member
          • Oct 2011
          • 3

          #5
          Try this

          Code:
          DECLARE @Email varchar(50)
          SET @Email = '"vini katyal" <vinikatyal@yahoo.com>'
          SELECT
          SUBSTRING(@Email,
          charindex('<',@Email),
          LEN(@Email)) as [email]
          Last edited by NeoPa; Oct 22 '11, 12:18 PM. Reason: Added mandatory [CODE] tags for you

          Comment

          • patjones
            Recognized Expert Contributor
            • Jun 2007
            • 931

            #6
            @clvandyke: Good stuff! I cut and pasted it into my SSMS window and it works nicely. It would be great if the OP can weigh in.

            Pat

            Comment

            • nbiswas
              New Member
              • May 2009
              • 149

              #7
              Try this

              Code:
              Declare @str varchar(50) = '"vini katyal" <vinikatyal@yahoo.com>'
              Solution 1
              -------------

              Code:
              Select 
              		Name  = Left(@str, PATINDEX('%<%',@str)-1)
              		,Domain = Right(@str,Len(@str)-PATINDEX('%<%',@str)+1)
              		,Email = @str
              Solution 2
              Code:
              Select 
              		Name  = Left(@str, CHARINDEX('<',@str)-1)
              		,Domain = Right(@str,Len(@str)-CHARINDEX('<',@str)+1)
              		,Email = @str
              Solution 3
              Code:
              Select 
              		Name  = REVERSE(STUFF(REVERSE(@str),1,PATINDEX('%<%',REVERSE(@str)),''))
              		,Domain = STUFF(@str,1,PATINDEX('%<%',@str)-1,'')
              		,Email = @str
              Solution 4
              Code:
              Select 
              		Name  = SUBSTRING(@str,1,CHARINDEX('<',@str,1)-1)
              		,Domain = Right(@str,Len(@str)-CHARINDEX('<',@str)+1)
              		,Email = @str
              In all the four cases the result is as under

              Code:
              Name	Domain	Email
              "vini katyal" 	<vinikatyal@yahoo.com>	"vini katyal" <vinikatyal@yahoo.com>
              Hope it helps

              Comment

              Working...