0

I want to extract data from MS Access database to MS Excel using VBA.

I know the code to extract but facing issues manipulating the column.

I want to extract string before first comma, and then string after second and before third comma, and then string after third comma.

As an example. Let's say I have one column called fruits and it has data as

Banana,Apple,Orange,Grapes

I want to create four columns which will have Banana on first, Apple on second, Orange on third and Grapes on fourth column.

Below is what I tried for first column.

SQL = "SELECT SUBSTRING(Fruits,0,CHARINDEX(',',Fruits) as column1 from tablename"
2
  • 2
    The best advice here is to stop storing CSV strings in your tables, especially when you consider each CSV value to be a single point of data. Instead, get each fruit string onto a separate record. Fruits love having some breathing room! Commented Nov 27, 2020 at 9:14
  • Due to some limitation, we can't create more columns on the tables. So, looking for these manipulation to get data on my excel. Commented Nov 27, 2020 at 9:22

2 Answers 2

1

Try this:

DECLARE @Data NVARCHAR(MAX) = 'Banana,Apple,Orange,Grapes'

DECLARE @DataXML XML = '<a>' + REPLACE('Banana,Apple,Orange,Grapes', ',', '</a><a>') + '</a>'

SELECT *
FROM
(
    SELECT T.c.value('.','varchar(255)')
          ,ROW_NUMBER() OVER (ORDER BY T.c)
    FROM @DataXML.nodes('/a') T(c)
) DS ([value], [col])
PIVOT
(
    MAX([value]) FOR [col] IN ([1], [2], [3], [4])
) PVT

SELECT 'Banana,Apple,Orange,Grapes'
 AS [my_column]
INTO [my_table]


WITH DataSource (DataXML) AS
(
    SELECT CAST('<a>' + REPLACE([my_column], ',', '</a><a>') + '</a>' AS XML)
    FROM [my_table] 
)

SELECT *
FROM
(
    SELECT T.c.value('.','varchar(255)')
          ,ROW_NUMBER() OVER (ORDER BY T.c)
    FROM DataSource
    CROSS APPLY [DataXML].nodes('/a') T(c)
) DS ([value], [col])
PIVOT
(
    MAX([value]) FOR [col] IN ([1], [2], [3], [4])
) PVT
Sign up to request clarification or add additional context in comments.

2 Comments

We can't declare variables as there is one field in my database which have thousands of rows. I want to write a query on the basis of field name and then extract.
@PraveenKUMAR You can use CTE if you don't want to declare variables. Do you want to show you how?
0

You can create a tiny helper function and use Split:

Public Function GetField( _
    ByVal AllFields As String, _
    ByVal Index As Integer) _
    As String
    
    GetField = Split(AllFields, ",")(Index)
    
End Function

and then:

SQL = "Select GetField([Fruit], 0) As Fruit From YourTable"

4 Comments

When I run this SQL query using vba in a procedure.. it says function doesn't exist. I have also created the tiny helper function outside.
If located in a code module, compiled and saved, it should be reachable.
it says undefined function.. I have saved in the same module
The same module as ..? But, try to create a new module and move the function to this.

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.