2

I have been searching for a solution for a while, and I find myself coming up empty handed.

The question is: Can you build a SQL query against a worksheet if you don't have a distinct header row for column references?

Easily enough:

  |     A     |     B
1 | FirstName | LastName
2 | John      | Davis
3 | Mary      | Parker

SELECT [LastName] FROM [Sheet1$] WHERE [FirstName] = 'John'

--> "Davis"

However, I tend to work with header-less CSV files, so what could I do if I don't have the header row to steer by?

  |     A     |     B
1 | John      | Davis
2 | Mary      | Parker
SELECT ??? FROM [Sheet1$] WHERE ??? = 'John'

To extend the question, it could equally interesting to know how to reference a row number - if possible - say I want to find the Last Name (column B) relative to row [2]

Thanks in advance for any input on my conundrum!

-B

2
  • Have you tried the "Microsoft Text Driver (*.txt; *.csv)" ODBC driver instead? Commented Feb 2, 2014 at 14:01
  • I haven't, but it's also an option. Thanks for commenting! Commented Feb 2, 2014 at 19:07

1 Answer 1

13

You can go through this link:

http://www.sql-server-helper.com/tips/read-import-excel-file-p02.aspx

Providing the HDR attribute as NO, will automatically name the columns as F1 to Fn.

1
  • 4
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFile & "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';" Commented Feb 2, 2014 at 20:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.