Avatar of johnnyg123
johnnyg123
Flag 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?
Microsoft SQL Server

Avatar of undefined
Last Comment
johnnyg123

8/22/2022 - Mon
Norie

What happens if you remove IMEX=1?
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
Norie

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
johnnyg123

ASKER
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 :-)
Norie

How did you try the cast?
ASKER CERTIFIED SOLUTION
johnnyg123

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.