Link to home
Start Free TrialLog in
Avatar of Kevin
KevinFlag for United States of America

asked on

How to split a value in to multiple columns within a command statement VB.NET?

Good Afternoon,

Can someone tell me how to do a split of a value in a column in to multiple columns within a datatable?

I have a table within an access 2010 database that I need to export. The export is actually working fine with what I have at the moment.

The problem I am having is that in one of my columns “DebitAcc” in the access table, has values like this 0184-510554-320, I need to split these values in to 3 columns (SUCURSAL, NUMERO_BA and SUFIJO).

I have attempted this by using the “InStr” syntax in my command statement.

LEFT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], " - ") 1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUFIJO

Open in new window


But doing this is giving me an error “InvalidCastException was Unhandled” for “Conversion from string "SELECT Format(ChqNum, '000000') " to type 'Double' is not valid.”. But I cant understand why this error is being thrown because if I don’t include the above line in the statement, the code runs fine.

Example below of the datatable when I don’t include the line above to split the value.

User generated image
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        'Initialize the objects before use
        Dim dataAdapter As New OleDbDataAdapter
        Dim dataSet As New DataSet
        Dim command As New OleDbCommand
        Dim dtMain As New System.Data.DataTable()
        Dim conn As New OleDb.OleDbConnection

        'Assign connection string to connection object
        conn.ConnectionString = sConnString()
        command.Connection = conn
        command.CommandType = CommandType.Text

        'Run Select Statement
        command.CommandText = "SELECT Format(ChqNum, '000000') As REFERENCIA, Format(ChqDate, 'mm/dd/yyyy') As FECHA, Format(Amount, 'Standard') As IMPORTE, Format(PayTo) As NARRATIVA1, Format(Reference) As NARRATIVA2, LEFT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], " - ") 1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUFIJO, Format(Currency) As DIVISA FROM tblSBTHist"
        dataAdapter.SelectCommand = command

        Try

            Dim dc As System.Data.DataColumn
            Dim dr As System.Data.DataRow
            Dim colIndex As Integer = 0
            Dim rowIndex As Integer = 0

            'Fill data to datatable
            conn.Open()

            dtMain.Columns.Add("SECUENCIA")
            dtMain.Columns.Add("SUCURSAL")
            dtMain.Columns.Add("NUMERO_BA")
            dtMain.Columns.Add("SUFIJO")
            dtMain.Columns.Add("IMPORTE")
            dtMain.Columns.Add("DIVISA")
            dtMain.Columns.Add("FECHA")
            dtMain.Columns.Add("TRANSACION")
            dtMain.Columns.Add("REFERENCIA")
            dtMain.Columns.Add("NARRATIVA1")
            dtMain.Columns.Add("NARRATIVA2")
            dtMain.Columns.Add("NARRATIVA3")
            dtMain.Columns.Add("NARRATIVA4")
            dtMain.Columns.Add("GRUPO_CONT")
            dtMain.Columns.Add("COD_REFER")
            dtMain.Columns.Add("COD_USER1")
            dtMain.Columns.Add("COD_USER2")
            dtMain.Columns.Add("COD_ERROR")
            dtMain.Columns.Add("DES_ERROR")


            dataAdapter.Fill(dtMain)


            conn.Close()

Open in new window


End result of what the split will do within the datatable should look like this:

User generated image
Kindly advise.

Regards,
N
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin

ASKER

Thanks for spotting the error MlandaT.