2

I am using Access DB and the data in the table are as follows

ID  Number
1   12,34,45,55,67,66,5,7
2   45,55,67,89,777
3   23,45,67,88,777,8888,564
4   1,234,567,890,987,650,000,000
5   222,233,345,645,634,000
6   33,44,55,66,77
7   12,34,22,88,99

The expected output that I am looking

Id  Number
1   12
1   34
1   45
1   55
1   67
1   66
1   5
1   7
2   45
2   55
2   67
2   89
2   777

I have accessdb and no other DB. requesting you to help me out with this issue as I am not aware of Accessdb. do let me know if you need any more details.

This is the query that I had written in Access db query so what it would do is that based on the number Id was getting updated in another table.

SELECT  t1.Id
        , t2.NUMBER 
FROM    table 1 AS t1 INNER JOIN table2 AS t2 ON t1.number LIKE t2.number & '*';
2
  • Hi, could you let us see the SQL or VBA code you've tried already please? Commented Jun 26, 2018 at 10:26
  • This is the query that i had written in access db query so what it would do is that based on the number Id was getting updated in another table. SELECT t1.Id, t2.NUMBER FROM table 1 AS t1 INNER JOIN table2 AS t2 ON t1.number LIKE t2.number & '*'; Commented Jun 26, 2018 at 10:30

2 Answers 2

1

This is a pretty complex task, but it can be achieved by creating a VBA function to get an item at a specific position, and then cross-joining either a pre-filled consecutive sequence table, or a sequence-generating query.

First, we declare the VBA function (from here, and I tell you why you can't use SQL for that there too):

Public Function SplitString(str As String, delimiter As String, count As Integer) As Variant
    Dim strArr() As String
    strArr = Split(str, delimiter, count + 1)
    count = count - 1 'zero-based
    If UBound(strArr) >= count Then
        SplitString = strArr(count)
    End If
End Function

Then, we use that with the sequence generating table/query to split the strings. In my example, I'll use this query by Gustav as sequence generator:

SELECT MyTable.ID,  SplitString(MyTable.[Number], ",", Sequence.[Value]) As [Number]
FROM 
    MyTable,
    (
        SELECT DISTINCT 
        [Tens]+[Ones] + 1 AS [Value], 
        10*Abs([Deca].[id] Mod 10) AS Tens, 
        Abs([Uno].[id] Mod 10) AS Ones
        FROM 
            msysobjects AS Uno, 
            msysobjects AS Deca;
    ) As Sequence
WHERE SplitString(MyTable.[Number], ",", Sequence.[Value]) Is Not Null
Sign up to request clarification or add additional context in comments.

Comments

0

I think the obvious initial question is why you're storing multiple "numbers" in one field: This goes against the whole point of using a relational database (even Access). Is there a reason you're storing comma-delimited values in one field? Storing each "number" in a separate field would certainly make your queries easier to construct.

Another question: Some of the numbers stored are "000". Are these valid codes, or do they signify no value (i.e. null)? How would they be handled in your output?

I think you might be better off, if possible, to create 2 separate tables: One to store the ID (and other associated attributes), and another table to store IDs for each entity. For example: tEntity could store each entity's ID (and name). tEntityValues would have a separate row for each number/code tied to a an entity. Then, your queries would be much simpler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.