การระบุตัวระบุข้อความขณะนำเข้าไฟล์ csv ไปยัง SQL Server DB

ฉันมีไฟล์ csv ที่คั่นด้วยเครื่องหมายจุลภาค ปัญหาคือมีเครื่องหมายจุลภาคอยู่ภายใน coloumns ด้วย

TEXT,["pn","service"]

TEXT อยู่ในคอลัมน์หนึ่งและ ["pn","service"] อยู่ในคอลัมน์อื่น

ฉันจะใช้ตัวระบุข้อความเพื่อแยกคอลัมน์อย่างถูกต้องได้อย่างไร ป้อนคำอธิบายรูปภาพที่นี่


person Kaja    schedule 29.08.2018    source แหล่งที่มา
comment
หากค่าเป็น '["pn","service"]' (ไม่ใช่ '"pn","service"') แสดงว่าไฟล์ของคุณไม่ใช่ไฟล์ CSV ที่ถูกต้อง หากคุณเป็นอักขระที่คุณใช้เพื่อกำหนดขอบเขตของคุณสามารถ/จะอยู่ในค่าได้ คุณ ต้อง ใช้ตัวระบุข้อความ หากไม่มี ตัวคั่น (ในกรณีของคุณ ,) ภายในค่าข้อความจะถือเป็นตัวคั่น สิ่งนี้จะเกิดขึ้นกับโปรแกรมอ่าน CSV ที่คุณมี หากคุณไม่สามารถเปลี่ยนแปลงได้ คุณจะต้องใช้งานสคริปต์ใน SSIS แทน และสร้างสิ่งทั้งหมดตั้งแต่ต้น   -  person Larnu    schedule 29.08.2018
comment
นอกจากนี้ หากคุณสามารถเปลี่ยนค่าให้มีตัวระบุเครื่องหมายคำพูดได้ อย่าใช้ '" เนื่องจากค่าดังกล่าวจะอยู่ในค่าข้อความของคุณด้วย หากทำได้ ฉันขอแนะนำให้เปลี่ยนตัวคั่นของคุณ ฉันพยายามใช้สิ่งต่าง ๆ เช่นไปป์ (|) เนื่องจากมันแทบจะไม่ปรากฏข้อมูลเลย   -  person Larnu    schedule 29.08.2018


คำตอบ (1)


เมื่อเร็ว ๆ นี้ฉันต้องจัดการกับสิ่งนี้และเสนอการล้อมฟิลด์ด้วยตัวระบุ ฉันแก้ไขมาโครด้านล่างเพื่อแยกตามไปป์แทนเครื่องหมายคำพูด โพสต์ superuser มีตัวเลือกเพิ่มเติม ลองดู https://support.microsoft.com/en-us/help/291296/procedure-to-export-a-text-file-with-both-comma-and-quote-delimiters-i และ https://superuser.com/questions/130592/how-do-you-force-excel-to-quote-all-columns-of-a-csv-file

อัปเดตตามคำแนะนำของ @ Larnu - สิ่งที่ฉันใช้เพื่อห่อไฟล์ข้อมูลบางไฟล์ใหม่ก่อนที่จะนำเข้าไฟล์เหล่านั้นสำหรับการโหลดครั้งเดียว เพียงระมัดระวังในการเปิด CSV ใน Excel โดยตรง... คุณอาจเห็นว่าค่าเปลี่ยนแปลง

Sub PipeCommaExport()
   ' Dimension all variables.
   Dim DestFile As String
   Dim FileNum As Integer
   Dim ColumnCount As Integer
   Dim RowCount As Integer

   ' Prompt user for destination file name.
   DestFile = InputBox("Enter the destination filename" _
      & Chr(10) & "(with complete path):", "Quote-Comma Exporter")

   ' Obtain next free file handle number.
   FileNum = FreeFile()

   ' Turn error checking off.
   On Error Resume Next

   ' Attempt to open destination file for output.
   Open DestFile For Output As #FileNum

   ' If an error occurs report it and end.
   If Err <> 0 Then
      MsgBox "Cannot open filename " & DestFile
      End
   End If

   ' Turn error checking on.
   On Error GoTo 0

   ' Loop for each row in selection.
   For RowCount = 1 To Selection.Rows.Count

      ' Loop for each column in selection.
      For ColumnCount = 1 To Selection.Columns.Count

         ' Write current cell's text to file with quotation marks.
         Print #FileNum, "|" & Selection.Cells(RowCount, _
            ColumnCount).Text & "|";

         ' Check if cell is in last column.
         If ColumnCount = Selection.Columns.Count Then
            ' If so, then write a blank line.
            Print #FileNum,
         Else
            ' Otherwise, write a comma.
            Print #FileNum, ",";
         End If
      ' Start next iteration of ColumnCount loop.
      Next ColumnCount
   ' Start next iteration of RowCount loop.
   Next RowCount

   ' Close destination file.
   Close #FileNum
End Sub
person TEEKAY    schedule 29.08.2018
comment
หากคุณกำลังโพสต์ลิงก์ คุณควรรวมส่วนที่ถูกต้องด้วย ลิงก์อาจใช้งานไม่ได้ แต่ข้อความที่คุณมีในโพสต์ของคุณจะไม่เป็นเช่นนั้น - person Larnu; 29.08.2018