Return sepcifc data on query

  • If I run a query to return an email address from an emails replied to, I get some returnes with multiple email addresses with the email address I want at the end.

    Example - Service, Customer Support, Customer123@provider.com

    How do I remove the info I don't want in these rows but not affect the other rows that are correct?

  • bquintana (6/26/2013)


    If I run a query to return an email address from an emails replied to, I get some returnes with multiple email addresses with the email address I want at the end.

    Example - Service, Customer Support, Customer123@provider.com

    How do I remove the info I don't want in these rows but not affect the other rows that are correct?

    Hi and welcome to the forums. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hopefully this helps understand what I am asking.

    SELECT [t].[TicketID] as "Ticket"

    ,.[TicketHistoryID]

    ,[ac].[STR_ACCOUNT_NUMBER]

    ,[om].[body]

    ,[t].[TicketBoxID]

    ,[tb].[Name] as "Ticket Group"

    ,[g].[GroupName]

    ,[t].[Subject]

    ,[t].[Contacts]

    ,[t].[DateCreated]

    FROM [Emails].[dbo].[Tickets] [t]

    left outer join

    [Emails].[dbo].[TicketBoxes] [tb] on [t].[TicketBoxID] = [tb].[TicketBoxID]

    left outer join

    [SYS_Live].[dbo].[ACCOUNT] [ac]

    on [t].[Contacts] collate SQL_Latin1_General_CP1_CI_AS

    = [ac].[STR_EMAIL_ADDRESS] collate SQL_Latin1_General_CP1_CI_AS

    left outer join

    [SYS_Live].[dbo].[vwAPPS_SUMMARY] [vwapp]

    on [t].[Contacts] collate SQL_Latin1_General_CP1_CI_AS

    = [vwapp].[STR_EMAIL_ADDRESS] collate SQL_Latin1_General_CP1_CI_AS

    Where

    [t].[datecreated] between '05-29-2013' and '05-30-2013'

    and .[TicketBoxID]in ('154','155','156','158','160','165')

    and [om].[body] not like '%auto-%'

    Order by [t].[DateCreated] desc

  • What would really help is:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    You are asking for help writing code against some sql tables but I don't have those tables to work with. From the excel spreadsheet I am guessing that the best way to accomplish is with a CLR proc. You will need to parse the values on commas first and then look for valid emails using a regex. I can help you figure this out but with no tables it is guess work on my part.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • What Sean said. This might help: http://www.sqlservercentral.com/articles/66909/

    However with regards to #3, explain how the results are calculated. In your example, I assume you want "Customer123@provider.com", but you never say that. Or do you want another value?

    What if there are multiple values in there? Or if this value is at the beginning? Do you only want the last email?

    To try and help you code this, we need some better understanding of the rules under which you are operating.

  • Sean Lange (6/26/2013)


    What would really help is:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    You are asking for help writing code against some sql tables but I don't have those tables to work with. From the excel spreadsheet I am guessing that the best way to accomplish is with a CLR proc. You will need to parse the values on commas first and then look for valid emails using a regex. I can help you figure this out but with no tables it is guess work on my part.

    Wouldn't the 8K Splitter function work along with one or two CHARINDEX?

    I'm not sure if it will get too complex depending on the OP requirements.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (6/26/2013)


    Sean Lange (6/26/2013)


    What would really help is:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    You are asking for help writing code against some sql tables but I don't have those tables to work with. From the excel spreadsheet I am guessing that the best way to accomplish is with a CLR proc. You will need to parse the values on commas first and then look for valid emails using a regex. I can help you figure this out but with no tables it is guess work on my part.

    Wouldn't the 8K Splitter function work along with one or two CHARINDEX?

    I'm not sure if it will get too complex depending on the OP requirements.

    I was thinking that we could use the 8k splitter in a CLR sproc and then use a .net regex to only get those that also have a valid email. This got me to thinking that I need to create my own CLR for validating emails though...thanks!!! 😀

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This is a learning experience on this one. Self taught and learning as I go.

    I'd like result to be the email address only and it is the last entry in the line seperated by commas, but only if there are other internal email address. Otherwise it is just the reply to email address.

    so the field result looks like any one of these examples.

    Customer Support, Customer45@hotmail.com

    s_prod_i3admin, s_prod_i3admin, Customer12@yahoo.com

    1234Customer@msn.com

    Thank you for your patience.

  • You'll have to provide mode DDL and sample data. Also, how do you define an internal address?

  • See if something like this will get you started. Please notice how I posted ddl and sample data in a consumable format.

    if OBJECT_ID('tempdb..#Something') is not null

    drop table #Something

    create table #Something

    (

    Ticket int,

    contacts varchar(100)

    )

    insert #Something

    select 4368642, 'Customer Support, Customer45@hotmail.com' union all

    select 4368640, 's_prod_i3admin, s_prod_i3admin, Customer12@yahoo.com' union all

    select 4368023, '1234Customer@msn.com'

    select *

    from #Something s

    cross apply dbo.DelimitedSplit8K(s.contacts, ',') x

    where CHARINDEX('@', x.Item) > 0

    You can find the code for the DelimitedSplit8K function by following the link in my signature about splitting strings.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Steve Jones - SSC Editor (6/26/2013)


    You'll have to provide mode DDL and sample data. Also, how do you define an internal address?

    Any internal email will not have the @provider.com.

  • Sean Lange (6/27/2013)


    See if something like this will get you started. Please notice how I posted ddl and sample data in a consumable format.

    if OBJECT_ID('tempdb..#Something') is not null

    drop table #Something

    create table #Something

    (

    Ticket int,

    contacts varchar(100)

    )

    insert #Something

    select 4368642, 'Customer Support, Customer45@hotmail.com' union all

    select 4368640, 's_prod_i3admin, s_prod_i3admin, Customer12@yahoo.com' union all

    select 4368023, '1234Customer@msn.com'

    select *

    from #Something s

    cross apply dbo.DelimitedSplit8K(s.contacts, ',') x

    where CHARINDEX('@', x.Item) > 0

    You can find the code for the DelimitedSplit8K function by following the link in my signature about splitting strings.

    Ok, I really appreciate the direction. Thanks again for you patience and help.

  • You are welcome. Hope that helps get you going.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply