none
Get first row in file

    Question

  • I have a large CSV file (4 GB).  I need to get the first row in the file.  I tried something like the following:

    @raw = SELECT content, ROW_NUMBER() OVER () RowNum
    FROM @extract;
    
    @out = SELECT content
     FROM @raw
    WHERE RowNum == 1;
    

    The row that was returned was some random row.

    Is there a way to get that first row?


    Russel Loski, MCSE Data Platform/Business Intelligence Twitter: @sqlmovers; blog: www.sqlmovers.com

    Thursday, June 29, 2017 10:02 PM

Answers

  • Hi,
    I suppose CSV file is a plain text file. If this is correct, you can use System.IO.StreamReader and read one line and close it.

    Ashidacchi

    • Marked as answer by Russ Loski Sunday, July 2, 2017 8:10 PM
    Friday, June 30, 2017 7:37 AM

All replies

  • Hi,
    I suppose CSV file is a plain text file. If this is correct, you can use System.IO.StreamReader and read one line and close it.

    Ashidacchi

    • Marked as answer by Russ Loski Sunday, July 2, 2017 8:10 PM
    Friday, June 30, 2017 7:37 AM
  • Thank you, Ashidacchi, will try that.

    I had a second reason to post this question, so I will leave it unanswered until tomorrow.  Being able to number lines in a file is useful in other situations.  I have some files where the order of lines in the file is important.  The file looks to be the results of a union of two queries.  The problem is that most of the information that comes from the second query is a duplicate of values that came from the first query.  I need to be able to distinguish which query a value comes and order in the file is the only tell.


    Russel Loski, MCSE Data Platform/Business Intelligence Twitter: @sqlmovers; blog: www.sqlmovers.com

    Friday, June 30, 2017 2:01 PM
  • I found a project that Mike Rys posted (https://github.com/Azure/usql/tree/master/Examples/FlexibleSchemaExtractor).  It provides me with what I need to work with.

    Russel Loski, MCSE Data Platform/Business Intelligence Twitter: @sqlmovers; blog: www.sqlmovers.com

    Sunday, July 2, 2017 8:11 PM