How to read a CSV with selective text qualifiers into a database using ASP Classic

I am using ASP Classic to read a CSV and import it into a database.  The data file I am given only has selective text qualifiers; i.e. if a field has a comma, the field is surrounded by a double quote.  Therefore, the file has both fields surrounded by double quotes and fields surrounded by nothing.

In other words, the file looks as follows:
John Doe,"123 Main St., Apt. 4",Athens,GA

How do I read this file properly into a DB when there are comma delimiters within the field?

For background, I am currently using a Excel macro called "Text Write Program" to convert the CSV into a pipe-delimited TXT, which my ASP script can read properly.  But I am looking to cut out this manual workaround.
Who is Participating?
MarblemanConnect With a Mentor Commented:
I guess it is a little bit tricky:

Split the line into an array using the "," separator

Walk trough the array and check the first character of every field. If it is a quote, remove the qoute and add this field and all following array fields to a new string until you find one field with a qoute as the LAST character.

While walking through the array you can build your SQL-Query...

try this

sorry, forgot to attach the code, try this
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

i give up!

            Dim MyDelimiter = ","
            Dim IsQuoted As Boolean = True
            Dim sr As New Microsoft.VisualBasic.FileIO.TextFieldParser("YourPath")
            sr.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            sr.Delimiters = New String() {MyDelimiter}
            sr.HasFieldsEnclosedInQuotes = IsQuoted
exactlyAuthor Commented:
nepaluz, how would I incorporate your solution into an ASP Classic page?
nepaluz wrote the code in VB or something else and you are probaly using VBScript.

If you can generate the Object (have the right dll - probably vbrun.dll - on your server) you are doing it that way:

sr = Server.CreateObject("Microsoft.VisualBasic.FileIO.TextFieldParser")

Howerver, you'll probably won't have the Microsoft.VisualBasic stuff available on your server.
That's why I think you'll be faster with a little code...

copy and paste.......?
(and I still have a straight face on too!)
use oledb provider for csv/text

you can get the data into a recordset using simple select query
exactlyAuthor Commented:
Marbleman, I am using your methodology for doing this and so far so good.  The only problem I'm having is assigning a variable name to the field.  I tried using f(fieldpos) as shown below but no luck.  Any suggestions?  By the way, I'm not a developer by trade, and I haven't tested/debugged, so and other tips are welcome!

dim strPath,objFso,objTxtStream,theLine,myArr,i
strPath = Server.MapPath("customer_out.csv")
Set objFso = createObject("Scripting.FileSystemObject")
Set objTxtStream = objFso.OpenTextFile( strPath )

while not objTxtStream.AtEndOfStream
theLine = objTxtStream.ReadLine
myArr = split(theLine, ",")
i = 0
adj = 0
fieldpos = 0

do while fieldpos<12
if isNull(myArr(i+fieldpos+adj)) then
      f(fieldpos) = ""
                  if  left(myArr(i+fieldpos+adj),1) = """" then
                  f(fieldpos) = replace(myArr(i+fieldpos+adj) & myArr(i+fieldpos+1+adj),"""","" )
                  adj = adj+1
                  f(fieldpos) = myArr(i+fieldpos+adj)
                  end if
end if
response.write f(fieldpos)

response.write "<br>"

exactlyAuthor Commented:
very helpful!
if you face any trouble in current method, try mine its easy.
if you question about the fieldpos and the fieldname is still open:

simply do it that way:


Split it the same way into an array called aFieldnames and you can access your fieldname


Or did I get you wrong on this?
exactlyAuthor Commented:
Marbleman, that fixed it.  Working 100% now.  Thanks for leading the way!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.