SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 OPENROWSET import problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

funk.phenomena
Posting Yak Master

Canada
120 Posts

Posted - 02/20/2013 :  10:25:05  Show Profile  Reply with Quote
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:

VENDOR
C12345
C12346
F12345
F12345

When I import it, it automatically converts the value to currency:

VENDOR
12345.00
12346.00
12345.00
12345.00


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

India
52325 Posts

Posted - 02/20/2013 :  10:37:50  Show Profile  Reply with Quote
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

Canada
120 Posts

Posted - 02/20/2013 :  10:50:24  Show Profile  Reply with Quote
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

Canada
120 Posts

Posted - 02/21/2013 :  11:49:41  Show Profile  Reply with Quote
Anyone else have a solution?
Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22772 Posts

Posted - 02/24/2013 :  03:43:14  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000