• Jason-368451 (8/6/2014)


    I have a CSV file that I bulk loaded into a single column staging table.

    I need a way to parse proper csv files that are comma delimited with optional quote text qualifiers

    as the data may have embedded commas.

    i.e. Jason,"900 N. May ST., #5"

    using the Jeff's splitter it would get split into 3 columns i.e. Jason as column 1 900 N. May ST. as column 2 and #5 as column 3

    I need it to keep the address together when there are quotes text qualifiers .

    I was able to get the function from this article to work on my dev machine "Reaping the benefits of the Window functions in T-SQL"

    http://www.sqlservercentral.com/articles/SQL+Server+2012/106903

    But I need to deploy this to a server running 2008 and that function uses syntax that is only valid in 2012.

    I would appreciate any help.

    Hey there. Another option is to use SQL CLR. I wrote a library called SQL#[/url] that contains a Stored Proc named SQL#.String_SplitResultIntoFields. This proc takes a single field from a result set and splits it. The split expression can accept Regular Expressions :-). Meaning, you can follow the example here and split your imported data in a way that accounts for both embedded commas and embedded text-qualifiers. The only thing that RegEx expression doesn't do is remove the text qualifiers from the field, but it might be possible to update the RegEx to do that. To be fair, this proc is in the Full version, not the Free version, but it does handle your situation quite cleanly. There is another proc called File_SplitIntoFields which reads directly from the import file so you can bypass BCP and its format files, but that option does not handle embedded newlines (but that might not be a problem anyway given those would probably break your existing import).

    Take care,

    Solomon..

    EDIT:

    The condensed RegEx from the link noted above is:

    (?x)\s*,\s*(?=(?:[^"]*"[^"]*")*[^"]*$)

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR