Solved

Take apostrophe mark out of excel with sql query

Posted on 2009-04-08
3
404 Views
Last Modified: 2012-05-06
I have a small problem.  When I run the following code it imports from an excel spreadsheet into a temp table in a sql DB.  Problem is that the excel file has apostrophe's in some of the address like so
118 Joe's Lane
this makes the import error out.  I am trying to make this automated so that I don't need to mess with any part of the operation.  Is there a way to run a query first to take out apostrophies from the excel spreadsheet before I import.  Thanks
Private Sub ImportAir()
        Dim connectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Personnel\Sad Listing.xls;Extended Properties='Excel 8.0;'"
        Dim connection As New OleDb.OleDbConnection(connectString)
        Dim command As New OleDb.OleDbCommand
        Dim myDataAdapter As New OleDb.OleDbDataAdapter
        Dim myDataSet As New DataSet
        connection.Open()
 
        command.Connection = connection
        command.CommandText = "select * from [sheet 1$]"
        myDataAdapter.SelectCommand = command
        myDataAdapter.Fill(myDataSet)
        myDataTable = New DataTable
        myDataTable = myDataSet.Tables(0)
        connection.Close()
 
        insertUpdateDelete("delete from tblTempPersonnel")
 
        For Each myDataRow In myDataTable.Rows
            sql = "insert tblTempPersonnel (SSN, Name, Grade, Status, HomePhone, Address, City, " _
                & "DOB, Unit, PasCode, YrsSvc, DAFSC, PAFSC, ETS) " _
                & "values " _
                & "('" & myDataRow(0) & "', '" & myDataRow(1) & "', '" & myDataRow(2) & "', '" & myDataRow(3) & "', '" & myDataRow(4) & "', '" & myDataRow(5) & "', " _
                & "'" & myDataRow(6) & "', '" & myDataRow(7) & "', '" & myDataRow(8) & "', '" & myDataRow(9) & "', '" & myDataRow(10) & "', '" & myDataRow(11) & "', " _
                & "'" & myDataRow(12) & "', '" & myDataRow(13) & "')"
 
            insertUpdateDelete(sql)
        Next
 
    End Sub

Open in new window

0
Comment
Question by:kdeutsch
3 Comments
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24101270
I am not an Excel expert but from .Net apps we use parameters in the sql statement and put the text data into the paramters, then special characters do not "break" the sql string.  Again I am not qualified to give you the syntax in Excel, but maybe this will give you something to look for.
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24101525
Try Replacing
"'" & myDataRow(6) & "',
with
"REPLACE( " & "'" & myDataRow(6) & "' ", '''', '')" & "',

in your code and give it a try.
0
 

Author Comment

by:kdeutsch
ID: 24106948
rrjegan17,

Thanks for the start in the right direction, I had to move it up a column because of the zero start and revised it as following and it works great.  Thanks
"'" & Replace(myDataRow(5), "'", "''") & "',
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Server group by 10 26
VB.net and sql server 4 33
Help with adding DLL file in Windows project 20 28
vb.net winforms sizing/resolution? 4 29
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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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