We help IT Professionals succeed at work.

issue with open row set accessing excel

johnnyg123
johnnyg123 asked
on
I am using a openrowset  to read an excel spreadsheet in sql 2014

SELECT LICENSE
      ,LAST
      ,FIRST
      ,MI
      ,DESIGNATION
      ,BIRTHDATE
      ,ADDRESS1
      ,ADDRESS2
      ,CITY
      ,STATE
      ,ZIP
      ,TELEPHONE
-- ,ORIGINALISSUEDATE
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;HDR=YES;IMEX=1;Database=\\testserver\c$\IDPRUpload\IDPRUpload.xlsx', 'select * from [IDPR$]')




all is working well except one of the columns in the excel spreadsheet has a mix of dates and phone numbers (if I don't include this column in select it works fine)

so it is treating everything as a date and thus when gets to value that is a phone number it is blowing up with error converting data type



I thought using imex=1 would prevent it from trying to identify column as date but still same error

I need to be able to return all the values as varchar to identify some input errors

Is there something else I need to do?
Comment
Watch Question

NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
What happens if you remove IMEX=1?

Author

Commented:
should mention started without the imex=1

The birthdate fieldhad null values and was getting error.  Adding imex=1 solved that problem but

not the issue with some values being phone numbers and other values being dates in ORIGINALISSUEDATE

Hope that explains my issue a little better
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Strange, IMEX=1 is supposed to stop ADO trying to guess the data type from the first few rows and importing everything as text.

What happens if you include all the columns in the SELECT and, this might be the hard bit, use a function/whatever to cast the type of the problem column to something appropriate?

In Excel you could do that using the Format function and in MS-SQL you would use CAST - not sure which one you would need/or would work in this case.

Author

Commented:
Agreed

I am confused that imex=1 not working

I did try to do cast and can't seem to get that to stop error

Guess that's what I need the help with :-)
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
How did you try the cast?
Not Exactly what I wanted but setting hdr to no actually avoided the error

First row has the column headings but  can work with that

SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;HDR=NO;IMEX=1;Database=\\testserver\c$\IDPRUpload\IDPRUpload.xlsx', 'select * from [IDPR$]')


even though I did eventually find my own solution did want to award Norie points for trying to help :-)