Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:46:31+00:00 2026-06-05T13:46:31+00:00

I ran a query on a MS SQL database using SQL Server Management Studio,

  • 0

I ran a query on a MS SQL database using SQL Server Management Studio, and some the fields contained new lines. I selected to save the result as a csv, and apparently MS SQL isn’t smart enough to give me a correctly formatted CSV file.

Some of these fields with new lines are wrapped in quotes, but some aren’t, I’m not sure why (it seems to quote fields if they contain more than one new line, but not if they only contain one new line, thanks Microsoft, that’s useful).

When I try to open this CSV in Excel, some of the rows are wrong because of the new lines, it thinks that one row is two rows.

How can I fix this?

I was thinking I could use a regex. Maybe something like:

/,[^,]*\n[^,]*,/

Problem with this is it matches the last element of one line and the 1st of the next line.

Here is an example csv that demonstrates the issue:

field a,field b,field c,field d,field e
1,2,3,4,5
test,computer,I like
pie,4,8
123,456,"7

8

9",10,11
a,b,c,d,e
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:46:32+00:00Added an answer on June 5, 2026 at 1:46 pm

    A simple regex replacement won’t work, but here’s a solution based on preg_replace_callback:

    function add_quotes($matches) {
        return preg_replace('~(?<=^|,)(?>[^,"\r\n]+\r?\n[^,]*)(?=,|$)~',
                            '"$0"',
                            $matches[0]);
    }
    
    $row_regex = '~^(?:(?:(?:"[^"*]")+|[^,]*)(?:,|$)){5}$~m';
    
    $result=preg_replace_callback($row_regex, 'add_quotes', $source);
    

    The secret to $row_regex is knowing ahead of time how many columns there are. It starts at the beginning of a line (^ in multiline mode) and consumes the next five things that look like fields. It’s not as efficient as I’d like, because it always overshoots on the last column, consuming the “real” line separator and the first field of the next row before backtracking to the end of the line. If your documents are very large, that might be a problem.

    If you don’t know in advance how many columns there are, you can discover that by matching just the first row and counting the matches. Of course, that assumes the row doesn’t contain any of the funky fields that caused the problem. If the first row contains column headers you shouldn’t have to worry about that, or about legitimate quoted fields either. Here’s how I did it:

    preg_match_all('~\G,?[^,\r\n]++~', $source, $cols);
    
    $row_regex = '~^(?:(?:(?:"[^"*]")+|[^,]*)(?:,|$)){' . count($cols[0]) . '}$~m';
    

    Your sample data contains only linefeeds (\n), but I’ve allowed for DOS-style \r\n as well. (Since the file is generated by a Microsoft product, I won’t worry about the older-Mac style CR-only separator.)

    See an online demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My SQL Server Management Studio suddenly went case sensitive on me. The database and
I ran this sql query in my database: update payments set method = 'paysafecard'
I have the following query that I ran on my database to remove some
I ran below SQL query against my MySQL database: load data local infile '/tmp/ad_packs.csv'
I'm just started using SQL and ran into a problem. In my database, I
I just ran into a SQL query with about 5 different columns in the
I ran into an interesting error with the following LiNQ query using LiNQPad and
I recently modified a MYSQL query using the keyword RLIKE however when I ran
My application runs SQL Server 2008 on Windows Server 2008 using Tomcat. I am
Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.