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
 General SQL Server Forums
 New to SQL Server Programming
 OPENROWSET import problem

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2013-02-20 : 10:25:05
Hi All - I'm using the OPENROWSET function which imports a CSV file into a table on the server.

There's a column on there which is numeric with a capitalized ALPHA character in front of it:
[CODE]
VENDOR
C12345
C12346
F12345
F12345
[/CODE]
When I import it, it automatically converts the value to currency:
[CODE]
VENDOR
12345.00
12346.00
12345.00
12345.00
[/CODE]

I've tried casting the value as nvarchar, but always drops the 1st character.

The only way around this is by manually replacing the capital letter with a small letter on the CSV file.

Any ideas? Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-20 : 10:37:50
change datatype by explicit casting to varchar in OPENROWSET

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2013-02-20 : 10:50:24
quote:
Originally posted by visakh16

change datatype by explicit casting to varchar in OPENROWSET


Here is my openrowset statement:

SELECT * FROM OPENROWSET ('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)};', 'SELECT * from G:\temp\AUDIT.CSV')

How can I go about an explicit cast? I've tried doing this and doesn't work:


SELECT * FROM OPENROWSET ('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)};', 'SELECT CAST(VENDOR AS VARCHAR) from G:\temp\AUDIT.CSV')

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2013-02-21 : 11:49:41
Anyone else have a solution?
Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-02-24 : 03:43:14
Have you tried this?

SELECT CAST(VENDOR AS VARCHAR(10)) FROM OPENROWSET ('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)};', 'SELECT * from G:\temp\AUDIT.CSV')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -