Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 BULK INSERT with differnt datatypes

Author  Topic 

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-11 : 16:51:40
How do i import from an textfile that has the follow syntax:

data.txt:

LogTime \t field1 \t field2 \r\n
2010-01-01 13:00:00 \t 13,141 \t -31,12

(quick note: \t here means tab-separated fields (0x09)
\n here mean 0x0A and \r is 0x0D)

...

The table is

CREATE TABLE dbo.[MyTable]
(
LogTime datetime,
field1 float null,
field2 float null
)


And for last the bulk insert command:

BULK INSERT MyTable
FROM 'c:\Data\data.txt'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)


Simply it doesn't work,
It gives error like:

"Bulk load data conversion error (type mismatch or invalid character for the specified codepage...."

so what am i doing wrong here, or have missing?

Thanks!
sincerely
V

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-11 : 18:04:02
Are you sure you don't need \r\n for ROWTERMINATOR?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-11 : 18:52:03
Hi

It doesn't seems to matter at all. I still got the conversion error.
I discovery that it doesn't work when it use type float or real here, of some reason.
I guess i have to specify a fmt file? (which i did hope to avoid, but...)
Or could the server still give an suggestion type automatically ?

/C
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-11 : 19:07:09
tkizer:

Another things did shows up now
You can use float type! BUT only if you use '.' instead of ',' in the numeric value!

But, how do i let it use ',' instead of '.' ?

example)
LogTime \t field1 \t field2 \r\n
2010-01-01 13:00:00 \t 13,141 \t -31,12

which now will have to be like:
LogTime \t field1 \t field2 \r\n
2010-01-01 13:00:00 \t 13.141 \t -31.12

In this case its ok to do it manually, but it is devastating when you have over 200 fields and atleast over thousand records!
what can be done in small steps in the bulk import (if it still can) for use ',' instead of '.' to an numeric value like this case?

Thanks again
/C

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-11 : 22:44:03
13.141 is not the same thing as 13,141. One is 13 and the other is 13 thousand. Can't you do a find/replace on the commas to an empty value? I've never had commas in my numeric data, so I've never had to encounter this before.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2010-04-13 : 09:13:34
quote:
Originally posted by tkizer

13.141 is not the same thing as 13,141. One is 13 and the other is 13 thousand.



Maybe it is to you, but there are plenty of locales where comma is the normal decimal separator.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-13 : 09:56:40
quote:
Originally posted by Arnold Fribble

quote:
Originally posted by tkizer

13.141 is not the same thing as 13,141. One is 13 and the other is 13 thousand.



Maybe it is to you, but there are plenty of locales where comma is the normal decimal separator.



Word!

Certainly causes about 1 problem a month for us with one of our Spanish clients.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-13 : 14:36:57
Interesting, didn't know that. Commas are only used here to visually separate the data 3 values at a time.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-13 : 18:37:24
Yes, it seems very common somehow.

It causes us atleast 5 problems a month (so i win i guess ;-) )

@tkizer: No sry sir, i cant do it manually , it needs to be done automatically unfortunate.
I dont want to change over millions of data by hand, im not that good handwriting-mode right now :)

Have you guys a hint or tip what can be done?

Thx!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-13 : 18:54:40
I'm not suggesting that you do it manually, but rather write VBScript to do the find/replace.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-13 : 19:10:27
Try this: http://blogs.technet.com/heyscriptingguy/archive/2005/02/08/how-can-i-find-and-replace-text-in-a-text-file.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-14 : 04:10:39
There's a common technique where you import your file into a staging table all as VARCHAR types. Then you can so your formatting in sql and insert into the real table from there. Something to consider?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-17 : 12:35:51
Thank you both, for answering!

Yes i will test both ideas and see which gives the best performance.

If i have time i maby can benchmark the result!
Will see.

Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-17 : 13:34:26
I'd be very interested in your performance results. I would bet that the VBScript way is faster as you don't have to move the data twice. But what Charlie describes is the most common approach that people use when importing data. It can be much easier to use T-SQL for SQL developers or DBAs, hence the reason to get the data into varchar columns.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-17 : 15:13:34
quote:
Originally posted by tkizer

Interesting, didn't know that. Commas are only used here to visually separate the data 3 values at a time.



Lots of interesting stuff on =Wikipedia as to why Period (USA) / Decimal point (UK & its Colonies) (a dot aligned vertically with the middle of the line) evolved, plus why the comma was used in France (Period was used in "print" to clarify Roman Numerals)

Furthermore, whilst USA/UK use the comma to separate the Thousands (and France et al use the period instead) other countries use narrow spaces, apostrophes and all sorts ... and in India they have a counting unit of 100,000 (lacs) so they write that as 1,00,000 and another unit for 100 lacs [a crore] which is 10,000,000 (in UK/USA format) but which I believe is written as 1,00,00,000 - very confusing!

The Japanese have a unit for 10,000 (a "Man") so they talk about "100 Man", which is 1 million in USA/UK parlance, which I found very hard to get my head around when I was living there. But I don't remember how they used commas in the numbers (Wikipedia suggest that both 10,000,000 and 1,000,0000 are valid in Japan!

I'll get my coat now ...
Go to Top of Page
   

- Advertisement -