Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1700
  • Last Modified:

BCP.exe SQL Import - Invalid character value for cast specification

Good afternoon,

I have a couple of flat files, with roughly 10K records that I need to import to my MS SQL DB once of twice a week.
I don't have access to the actual server, just access via Management Studio.

So... I am using the BCP command to run this import.  For now, I am testing this out on my local machine before using it in production.
The syntax is (sanitised):

bcp db.dbo.tableOld in E:\datafile.txt -c -r "0x0a" -S myserver-x

It gets through about 50+ records, then gives me the following error:
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 11 for SQL Server]Invalid character value for cast specification
(10 of them, before failing)

I suspect this may have to do with the date field?

Anyway... not sure how to address?

Any assistance would be greatly appreciated.

Thanks,

E.D.
0
Frank Grimes
Asked:
Frank Grimes
  • 4
  • 3
2 Solutions
 
funwithdotnetCommented:
I typically try to import data from a text file into varchar() fields and transform the data one imported. I find it to be easier to handle import issues.

Good luck!
0
 
ZberteocCommented:
Can you add a data sample from the flat file that fails? It could also be an encoding issue.

If you use -c switch by itself the BCP assumes tab as a field delimiter. If other character is the delimiter you need to add -t switch, i.e. for comma you will use -t,

Also you should remove the -r"0x0a" unless you know for sure that the row separator is only the new line character, \n, and not carriage return as well, \r!. If ignored the BDP by default will consider both as row separator.
0
 
Frank GrimesAuthor Commented:
Thanks for the advise guys... I found the issue, it wasn't date at all. There are 4 fields that were FLOAT. I changed them to varchar and the import worked perfectly. The only issue is... after I import, I try to change back to FLOAT and I get the following error:

- Unable to modify table.  
Error converting data type nvarchar to float.

Any thoughts? Thanks again guys!
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Frank GrimesAuthor Commented:
I think I found it... the some of the FLOAT values - 10-20, were letters. I assume that's why it wouldn't convert?! :-)
0
 
ZberteocCommented:
Varchar and Nvarchar datatypes "swallow" anything so there is no guarantee that the values imported in them are numerics that can be converted to FLOAT. In other words the input data could contain non numeric values either by mistake or it is just that.
0
 
ZberteocCommented:
It is possible to have letters in FLOAT values:

6.02E+12

this is a valid FLOAT value.
0
 
ZberteocCommented:
You can identify the non numeric values with:

select * from YourTable where isnumeric(nvarchar_colum)=0
0
 
Frank GrimesAuthor Commented:
I think I've got it, thanks guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now