Solved

How do you delete duplicate data in a string

Posted on 2011-02-12
9
309 Views
Last Modified: 2012-05-11
Hello,

I have an ACCESS table with duplicate data in a string, for example:

ROW1: BEL,BEL,CAN,CZE,DEU
ROW2: CAN,CZE,DEU,USA,USA
ROW3:BEL,FRA,FRA,CZE,DEU

How do you loop through the table and delete the duplicate countries.

Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 

Author Comment

by:vcharles
ID: 34879684
Hello,

Below is the code I'm using to create the string of countries, is there a way to modify it to avoid duplicate countries? The problem is NSNs are sometimes duplicated which causes multiple countries to be included in the string.

Thanks.

Victor

  Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=test"

        Dim objConnection As New OleDb.OleDbConnection(ConnectionString)
        objConnection.Open()

        'Dim da As New OleDb.OleDbDataAdapter("Select NIIN, code3 from IDENTIFICATION_DATA", objConnection)
        Dim da As New OleDb.OleDbDataAdapter("Select NSN, COUNTRYALL from AOP5", objConnection)

        Dim dt1 As New DataTable

        da.Fill(dt1)

        da.Dispose()
        da = Nothing

        'da = New OleDb.OleDbDataAdapter("Select NIIN, RN from REFERENCE_NUMBER_DATA", objConnection)
        da = New OleDb.OleDbDataAdapter("Select NSN, COUNTRY from AOP5A", objConnection)


        Dim dt2 As New DataTable

        da.Fill(dt2)

        Dim dv2 As DataView

        dv2 = dt2.DefaultView

        Dim cmd As OleDbCommand

        Dim SQLStr As String


        For Each dr As DataRow In dt1.Rows

            '.RowFilter = "NIIN='" & dr.Item("NIIN") & "'"
            dv2.RowFilter = "NSN='" & dr.Item("NSN") & "'"

            If dv2.Count > 0 Then


                SQLStr = "Update AOP5 Set COUNTRYALL='"

                For Each drv2 As DataRowView In dv2

                    SQLStr &= drv2.Item("COUNTRY") & ","
                Next
                'strip off trailing comma
                SQLStr = SQLStr.Substring(0, SQLStr.Length - 1)
                'add closing apostrophe
                SQLStr &= "'"
                'add WHERE clause
                SQLStr &= " Where NSN='" & dr.Item("NSN") & "'"
                ' MsgBox(SQLStr)
                cmd = New OleDbCommand(SQLStr, objConnection)
                'MsgBox(SQLStr)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                cmd = Nothing
            End If
        Next
0
 
LVL 9

Expert Comment

by:sshah254
ID: 34879722
You will not be able to do it in SQL.  You'll have to resort to VBA.

In VBA, get each row (or field) into a string, convert it to an array using the split function, sort the array (this will need to be written as a VBA function since there is no inbuilt sort), then select the unique values and do what you need to do with that ... either store them, or "join" them back into a string and put it back into the table, or print it, or whatever.

Ss
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34879801
Change this bit

For Each drv2 As DataRowView In dv2

                    SQLStr &= drv2.Item("COUNTRY") & ","
                Next


to


For Each drv2 As DataRowView In dv2
    If SQLStr.Contains(drv2.Item("Country")) = False Then
       SQLStr &= drv2.Item("COUNTRY") & ","
    End If
Next
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:vcharles
ID: 34879924
Thank you for the solution. Is there a way to sort the countries in the string? For example, in a row contains the following the data: BEL,USA,FRA,CAN.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34879947
Yes. Try this

Dim dTable As New DataTable
dTable.Columns.Add("Country", GetType(String))
For Each drv2 As DataRowView In dv2
    dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country")
    If dTable.DefaultView.Count = 0 Then
       Dim dRow as DataRow = dTable.NewRow()
       drow(0) = drv2.item("Country")
       dTable.Rows.Add(drow)
    End If
    dTable.DefaultView.RowFilter = ""
Next

For i As Integer = 0 to dTable.Rows.Count - 1
     SQLStr &= dTable.Rows(i).Item(0) & ","
Next
0
 

Author Comment

by:vcharles
ID: 34880011
Thanks for the code. I included it at the end of the existing code but I received the following  error on the deign mode: "Variable SQLStr is used before it has been assigned a value" and when I run the project I received the folowing error: "The expression contains an invalid string constant: 'GRC." on line: dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country").

    Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"

        Dim objConnection As New OleDb.OleDbConnection(ConnectionString)
        objConnection.Open()

        'Dim da As New OleDb.OleDbDataAdapter("Select NIIN, code3 from IDENTIFICATION_DATA", objConnection)
        Dim da As New OleDb.OleDbDataAdapter("Select FIF, COUNTRYFUZEALLA from AOP5", objConnection)

        Dim dt1 As New DataTable

        da.Fill(dt1)

        da.Dispose()
        da = Nothing

        'da = New OleDb.OleDbDataAdapter("Select NIIN, RN from REFERENCE_NUMBER_DATA", objConnection)
        da = New OleDb.OleDbDataAdapter("Select FIF, COUNTRY from AOP5A", objConnection)


        Dim dt2 As New DataTable

        da.Fill(dt2)

        Dim dv2 As DataView

        dv2 = dt2.DefaultView

        Dim cmd As OleDbCommand

        Dim SQLStr As String


        For Each dr As DataRow In dt1.Rows

            '.RowFilter = "NIIN='" & dr.Item("NIIN") & "'"
            dv2.RowFilter = "FIF='" & dr.Item("FIF") & "'"

            If dv2.Count > 0 Then


                SQLStr = "Update AOP5 Set COUNTRYFUZEALLA='"

                ' For Each drv2 As DataRowView In dv2

                'SQLStr &= drv2.Item("COUNTRY") & ","
                'Next
                For Each drv2 As DataRowView In dv2
                    If SQLStr.Contains(drv2.Item("COUNTRY")) = False Then
                        SQLStr &= drv2.Item("COUNTRY") & ","
                    End If
                Next
                'strip off trailing comma
                SQLStr = SQLStr.Substring(0, SQLStr.Length - 1)
                'add closing apostrophe
                SQLStr &= "'"
                'add WHERE clause
                SQLStr &= " Where FIF='" & dr.Item("FIF") & "'"
                ' MsgBox(SQLStr)
                cmd = New OleDbCommand(SQLStr, objConnection)
                'MsgBox(SQLStr)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                cmd = Nothing
            End If
        Next
        Dim dTable As New DataTable
        dTable.Columns.Add("Country", GetType(String))
        For Each drv2 As DataRowView In dv2
            dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country")  **Error Line
            If dTable.DefaultView.Count = 0 Then
                Dim dRow As DataRow = dTable.NewRow()
                dRow(0) = drv2.Item("Country")
                dTable.Rows.Add(dRow)
            End If
            dTable.DefaultView.RowFilter = ""
        Next
        For i As Integer = 0 To dTable.Rows.Count - 1
            SQLStr &= dTable.Rows(i).Item(0) & "," ****Error line
        Next
        MsgBox("COMPLETED")
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34880110
Change this

dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country").


to

dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country") & "'"


Also, the code I provide needs to replace this code

                For Each drv2 As DataRowView In dv2
                    If SQLStr.Contains(drv2.Item("COUNTRY")) = False Then
                        SQLStr &= drv2.Item("COUNTRY") & ","
                    End If
                Next
0
 

Author Comment

by:vcharles
ID: 34880334
I replaced the code as shown below, but it still doesn't sort to countries.

For Each dr As DataRow In dt1.Rows

            '.RowFilter = "NIIN='" & dr.Item("NIIN") & "'"
            dv2.RowFilter = "FIF='" & dr.Item("FIF") & "'"

            If dv2.Count > 0 Then


                SQLStr = "Update AOP5 Set COUNTRYFUZEALLA='"

                ' For Each drv2 As DataRowView In dv2

                'SQLStr &= drv2.Item("COUNTRY") & ","
                'Next

                'For Each drv2 As DataRowView In dv2
                'If SQLStr.Contains(drv2.Item("COUNTRY")) = False Then
                'SQLStr &= drv2.Item("COUNTRY") & ","
                'End If
                'Next

                For Each drv2 As DataRowView In dv2
                    If SQLStr.Contains(drv2.Item("COUNTRY")) = False Then
                        SQLStr &= drv2.Item("COUNTRY") & ","
                    End If
                Next

                'strip off trailing comma
                SQLStr = SQLStr.Substring(0, SQLStr.Length - 1)
                'add closing apostrophe
                SQLStr &= "'"
                'add WHERE clause
                SQLStr &= " Where FIF='" & dr.Item("FIF") & "'"
                ' MsgBox(SQLStr)
                cmd = New OleDbCommand(SQLStr, objConnection)
                'MsgBox(SQLStr)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                cmd = Nothing
            End If
        Next
        Dim dTable As New DataTable
        dTable.Columns.Add("Country", GetType(String))
        For Each drv2 As DataRowView In dv2
            dTable.DefaultView.RowFilter = "Country='" & drv2.Item("Country") & "'"

            If dTable.DefaultView.Count = 0 Then
                Dim dRow As DataRow = dTable.NewRow()
                dRow(0) = drv2.Item("Country")
                dTable.Rows.Add(dRow)
            End If
            dTable.DefaultView.RowFilter = ""
        Next
        For i As Integer = 0 To dTable.Rows.Count - 1
            SQLStr &= dTable.Rows(i).Item(0) & "," '****Error line
        Next

Thanks.
0
 

Author Closing Comment

by:vcharles
ID: 34882624
Thank You.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question