Link to home
Start Free TrialLog in
Avatar of johnnyg123
johnnyg123Flag for United States of America

asked on

issue with open row set accessing excel

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?
Avatar of Norie
Norie

What happens if you remove IMEX=1?
Avatar of johnnyg123

ASKER

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
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.
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 :-)
How did you try the cast?
ASKER CERTIFIED SOLUTION
Avatar of johnnyg123
johnnyg123
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial